What is “Dim fso, MyFile, FileName, TextLine” in VBA?

微笑、不失礼 提交于 2019-12-13 02:34:43

问题


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.


回答1:


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.




回答2:


All that line is doing, is defining those as variables to be used further down in the code

Also refer to this post on StackOverflow: What does DIM stand for in Visual Basic and BASIC?




回答3:


Dim fso, MyFile, FileName, TextLine

This line defines the variables.
The purpose of doing so it to help catch typos as the variables are referenced throughout the script. This is typically used in tandem withe Option Explicit (usually found at the top of the script).

By default, VBA doesn't require that variable be defined. One can override this [silly] default behavior by using the option Option Explicit so that an "undefined variable" exception be produced when a particular variable is not defined.
Without this setting, in the question's snippet if for example on line 4 we had inadertently typo-ed the name FileNam, omitting the e, VBA would proceed, having effectively two variables FileName and FileNam; later on in the program, when using, correctly, the variable FileName, a empty value would be used, leading to subtle and hard to find bugs.




回答4:


That 3rd line simply defines them to be used later on. Fso, Filename etc are simply placeholder variables to be used later in the code. fso is declared and set to a new file system object. This could be any type of file system - NTFS, FAT etc, but all it means is that you're about to be working with the files on the system. Then you use it to open the file specified for reading only, and away the rest of the code goes. You need to specify the fso so that the program knows where to read from - be it a file, an input stream or a separate additional file system.

I hope that helps somewhat!



来源:https://stackoverflow.com/questions/1722196/what-is-dim-fso-myfile-filename-textline-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!