Consulting

Results 1 to 8 of 8

Thread: Solved: Read smth from tex file. How ?

  1. #1
    VBAX Regular
    Joined
    Mar 2005
    Location
    Latvia; Germany
    Posts
    19
    Location

    Solved: Read smth from text file. How ?

    Hello friends !
    So subj:
    I try to run scripts from VBA but it's not working:
    [VBA]Sub OpenTextFileTest()
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, TristateFalse)
    f.Write "Hello world!"
    f.Close
    End Sub[/VBA]

    However I need to get numbers from Text file.
    So try to modify another example from VBA help:

    [VBA]Sub TextStreamTest()
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    Dim fs, f, ts, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CreateTextFile "c:\test1.txt" 'Create file
    Set f = fs.GetFile("c:\test1.txt")
    Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
    s = ts.ReadLine
    MsgBox s
    ts.Close
    End Sub[/VBA]

    I think I need not create file but just open it. But can't find procedure for it !

    Regards,
    separator
    Last edited by Killian; 04-21-2005 at 06:57 AM. Reason: Added VBA Tags

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    If you have a file already you just need to define it's path and open it[VBA]Sub TextStreamTest()

    Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
    Dim fs, f, ts, s


    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFilePath)
    Set ts = f.OpenAsTextStream(1, -2)
    s = ts.ReadLine
    MsgBox s
    ts.Close

    End Sub[/VBA]This will read the first line of the text file. If you want to read other lines then just loop through until the line number and return the last one[VBA]Sub TextStreamTest2()

    Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
    Dim fs, f, ts, s
    Dim i As Long


    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFilePath)
    Set ts = f.OpenAsTextStream(1, -2)
    For i = 1 To InputBox("Enter line number:", "Read from text file")
    s = ts.ReadLine
    Next
    MsgBox s
    ts.Close

    End Sub[/VBA]
    K :-)

  3. #3
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi separator,

    Your examples look more like vbscript examples than in vba (due to the const statements). But nonetheless, I can show you how to do what you need:

    [vba] Set ts = fs.OpenTextFile("C:\test1.txt", ForReading)
    'Do Until ts.AtEndOfStream
    s = ts.ReadLine
    MsgBox s
    'Loop
    ts.Close[/vba]

    I've commented out the part that will loop through the entire text file to be read. You could also use ts.ReadAll to input the entire text file.

    Why not just use the Open method build into vba/vb? If you're only going into a specific file and not needing to recurse subdirectories (the only really useful point of FSO) then the build in functions would probably be better. They are a little quicker, and you can do the same things:

    [vba] Open "C:\test1.txt" For Input As #1
    'Do Until EOF(1)
    Line Input #1, s
    MsgBox x
    'Loop
    Close #1[/vba]

    Please let us know if you have any specific questions about FSO or any of these file-accessing methods!
    Matt

  4. #4
    VBAX Regular
    Joined
    Mar 2005
    Location
    Latvia; Germany
    Posts
    19
    Location
    Thanx a lot for all!
    I try all your examples it's working well!
    I just don't know how it work in VBA! New language, just started whith it!
    Now I know!

    Regards!

  5. #5
    VBAX Regular
    Joined
    Mar 2005
    Location
    Latvia; Germany
    Posts
    19
    Location
    Hei one more question. How to write smth in file in the same procedure ? How i can understand it's not ok ? :

    [vba]Sub TextStreamTest()

    Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
    Dim fs, f, ts, s , m

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFilePath)
    Set ts = f.OpenAsTextStream(1, -2)
    s = ts.ReadLine
    MsgBox s
    m = "500"

    ts.write m

    ts.Close

    End Sub[/vba]

  6. #6
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    You'll need to close the file (it's still in read mode), them open it to append a line on the end.
    Also, don't forget to change the file path from my desktop path to your file's location or it won't find it!!![VBA]Sub TextStreamTest()

    Const strFilePath As String = "C:\Documents and Settings\Killian\Desktop\MY_TEXT_FILE.TXT"
    Dim fs, f, ts, s, m

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFilePath)
    Set ts = f.OpenAsTextStream(1, -2) 'open mode "For Reading"
    s = ts.ReadLine
    ts.Close
    MsgBox s

    m = "500"
    Set ts = f.OpenAsTextStream(8, -2) 'open mode "For Appending"
    ts.WriteLine m
    ts.Close

    End Sub[/VBA]P.S. I deleted the other duplicate thread for this question
    K :-)

  7. #7
    VBAX Regular
    Joined
    Mar 2005
    Location
    Latvia; Germany
    Posts
    19
    Location
    Thanx Killian!
    Can also said me how to delete information in file before put smth new in it ?
    Also where I can find what else operations I can do with file in VBA? (rename, move, delete, edit, etc.)

    Thanx!

  8. #8
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    If you open the file for Write instead of Append it will replace all the text in the file with what you write in.
    The best thing for you to do is to learn about the FileSystemObject - there's a lot you can do!
    Here's a good link:
    http://msdn.microsoft.com/library/de...jsfsotutor.asp
    K :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •