What is "Dim fso, MyFile, FileName, TextLine" in VBA?

brilliant picture brilliant · Nov 12, 2009 · Viewed 11.4k times · Source

I received this code from one of those nice people here who are willing to spend their time and energy to share their knowledge with noobs:

Sub ReadLinesFromAFileOneAfterAnother ()
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
End Sub

While I know what task this code performs, I would still want to know what each of its elements means and does. Can anyone, please, explain to me what the third line of this code is all about:

Dim fso, MyFile, FileName, TextLine

What is "fso" in the first place? I know it stands for "File System Object", but it does little to explain to me what it actually is and what it accomplishes. What do those three following words mean ("MyFile", "FileName", "TextLine")? Are they some kind of parameters of something?

I've read this: http://msdn.microsoft.com/en-us/library/h7se9d4f(VS.85).aspx

and this: http://msdn.microsoft.com/en-us/library/ebkhfaaz(VS.85).aspx

but it feels like those materials are wrriten for those who themselves would be able to write them - I hardly understood anything. Some things, of course, are more or less clear, but there are so many other terms and words that I don't know! Eventually, there isn't one whole complete and clear picture.

So, I gave up and decided to come back here. This site is probably one of the few on the internet (in fact I haven't met any other) that has declared in its rules: "No question is too trivial or too "newbie"". This gives me a kind of ground for asking this present question.

So, please, anyone, explain to me in simple terms what "fso" is. Precisely, what the third line of the code above is all about.

Thank you all in advance.

Answer

Rich picture Rich · Nov 12, 2009

The line of code:

Dim fso, MyFile, FileName, TextLine

declares things called 'variables' of type variant.

A variable is a bit of space in memory with a name and a type. You use them to let the program know you're going to be making use of them later on in the code.

Normally you'd give the variables a type (like integer, or string) but the coder hasn't, so it has defaulted to variant, which can take on any type (in essence).

Once you've done:

Set fso = CreateObject("Scripting.FileSystemObject")

then fso contains a bit of code that can do stuff to the file system.

Set MyFile = fso.OpenTextFile(FileName, ForReading)

Means you're using the fso functionality to open a filename you specify in the 'filename' variable, and you've put a reference to it in the 'myfile' variable.

So you can then do further stuff with the file by using the myfile variable.

The 'do while' loop reads through that file one line at a time (myfile.readline) and puts the result into the 'textline' variable, which holds a different line of text from the file every time you go round the loop, till the file is finished.

The idea of this code is to read the file line by line, doing stuff with the contents of each line as you come across it. You could print it, log it, display it to the user, etc, as the title of the sub indicates!

To be honest the basics about VB are essential for you to be able to interpret such code so I would suggest looking for an online tutorial or a book.