什么是 VBA 中的“Dim fso、MyFile、FileName、TextLine”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1722196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:59:01  来源:igfitidea点击:

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

vbascriptingcreateobjectfilesystemobject

提问by brilliant

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?

首先什么是“fso”?我知道它代表“文件系统对象”,但它几乎无法向我解释它究竟是什么以及它完成了什么。以下三个词是什么意思(“MyFile”、“FileName”、“TextLine”)?它们是某种东西的某种参数吗?

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

我读过这个:http: //msdn.microsoft.com/en-us/library/h7se9d4f(VS.85).aspx

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

和这个: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.

所以,请任何人用简单的术语向我解释“fso”是什么。确切地说,上面代码的第三行是关于什么的。

Thank you all in advance.

谢谢大家。

回答by Rich

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.

然后 fso 包含一些可以对文件系统执行操作的代码。

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.

意味着您正在使用 fso 功能打开您在 'filename' 变量中指定的文件名,并且您已将对其的引用放入 'myfile' 变量中。

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

因此,您可以使用 myfile 变量对该文件做进一步的处理。

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.

“do while”循环一次一行地读取该文件(myfile.readline),并将结果放入“textline”变量中,每次循环时,该变量都会保存来自文件的不同文本行,直到文件完成。

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.

老实说,有关 VB 的基础知识对于您能够解释此类代码至关重要,因此我建议您寻找在线教程或书籍。

回答by Anthony Shaw

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?

另请参阅 StackOverflow 上的这篇文章:DIM 在 Visual Basic 和 BASIC 中代表什么?

回答by mjv

Dim fso, MyFile, FileName, TextLine

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

这一行定义了变量。
这样做的目的是帮助捕捉错别字,因为在整个脚本中都引用了变量。这通常与Option Explicit(通常位于脚本顶部)一起使用。

By default, VBA doesn't requirethat variable be defined. One can override this [silly] default behavior by using the option Option Explicitso 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.

默认情况下,VBA 不要求定义该变量。可以通过使用该选项来覆盖这种 [愚蠢的] 默认行为,Option Explicit以便在未定义特定变量时产生“未定义变量”异常。
如果没有这个设置,在问题的片段中,例如在第 4 行,我们无意中输入了名称 FileNam,省略了 e,VBA 将继续进行,实际上有两个变量 FileName 和 FileNam;稍后在程序中,当正确使用变量 FileName 时,将使用空值,从而导致微妙且难以发现的错误

回答by Mark Mayo

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.

第 3 行简单地定义了它们以供稍后使用。Fso、Filename 等只是稍后在代码中使用的占位符变量。fso 被声明并设置为一个新的文件系统对象。这可以是任何类型的文件系统 - NTFS、FAT 等,但这意味着您将要处理系统上的文件。然后你用它打开指定为只读的文件,剩下的代码就消失了。您需要指定 fso 以便程序知道从哪里读取 - 无论是文件、输入流还是单独的附加文件系统。

I hope that helps somewhat!

我希望能有所帮助!