views:

168

answers:

3

This code will read a line from a text file:

set file = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\number.txt", 1)
text = file.ReadLine
MsgBox text

How can I make it read repeatedly one line after another from the same file? I guess, I should use a loop here, right? I need it to read the first line from the file at the first iteration, the second line at the second iteration, the third one at the third and so on till all the lines have been read. How can I do it?

Important addition: I need the code to operate on each line one by one - not all at once!

+3  A: 

Use the ReadAll() method:

text = file.ReadAll

(Might be of interest: FileSystemObject Sample Code)

With a loop:

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, MyFile, FileName, TextLine

Set fso = CreateObject("Scripting.FileSystemObject")

FileName = "c:\testfile.txt"

Set MyFile = fso.OpenTextFile(FileName, ForReading)

'' Read from the file
Do While MyFile.AtEndOfStream <> True
    TextLine = MyFile.ReadLine

    '' Do stuff to TextLine

Loop
MyFile.Close
Mitch Wheat
WOW!!! Thank you, Mitch!
brilliant
But I need to read lines one by one, not all of them at once, as I want o operate on each line differently.
brilliant
maybe you should add that info. to your problem description...
Mitch Wheat
Thank you Mitch. I am sorry I didn't mention this in my problem description right away. Can you, please, edit your answer a bit (make some minor changes like, perhaps, adding a word or a character that would not change anything), as I wnot to cast a positive vote on your answer, but a box pops up saying: "Vote too old to be changed, unless the answer is edited."
brilliant
I am sorry, I meant "I WANT to cast a positive vote...", of course.
brilliant
edited..........
Mitch Wheat
A: 

You can add a reference* to the Windows Script Host Object Model, it will help you with the FileSystemObject Object, because you can then say:

Dim fs As FileSystemObject
Dim f As TextStream

Which will allow you to use intellisense to see the various properties and the Object Browser to explore the library.

* In the code window, choose Tools, References and tick the box beside the library you want.

Remou
I am sorry, it's too complicated for me - I am just a noob. I am sure, though, that there should be some way in VBA, something like realall feature.
brilliant
Mitch Wheat has given you the answer, this is additional comments, as you mentioned in an earlier post that you wished to learn VBA. The Object Browser is a great help to learning, as is intellisense.
Remou
+1  A: 

If for some reason you want to use the in-built VBA file handling routines, you would use code like this:

Sub ReadAFileLineByLine()
    Dim InStream As Integer
    InStream = FreeFile()
    Open "C:/tmp/fastsynchtoquesttry_quest.txt" For Input As InStream

    Dim CurrLine As String
    Do While True
        Line Input #InStream, CurrLine
        ' do stuff to CurrLine
        If EOF(InStream) Then Exit Do
    Loop

    Close #InStream
End Sub
Jon Fournier
Thank you, Jon. I am quite surprised - it seems that you've been to another question of mine and then came here to share this code. (I guessed it by the first line, which I created by myself and which wasn't here in Mitch Wheat's code on this page.) Thank you for this code that you have shared here. It will still take me some time to study through it, but I'll be sure to do it.
brilliant
Just a coincidence, I think...I just name subs the best I can, but I'm glad someone else thinks like me! Cool to see you're doing powerpoint vba, it's always interesting when I do that...
Jon Fournier