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

发布于 2024-08-11 07:31:28 字数 1371 浏览 4 评论 0 原文

我从这里的一位好人那里收到了这段代码,他们愿意花时间和精力与菜鸟分享他们的知识:

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

虽然我知道这段代码执行什么任务,但我仍然想知道它的每个元素的含义和作用。任何人都可以向我解释一下这段代码的第三行是什么:

Dim fso, MyFile, FileName, TextLine

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

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

和这个: http://msdn.microsoft.com/en-us/library/ebkhfaaz(VS.85).aspx

但感觉这些材料是为那些自己能够编写的人编写的他们——我几乎什么都听不懂。当然,有些事情或多或少是清楚的,但还有很多其他术语和单词我不知道!最终,没有一幅完整、清晰的图景。

于是,我放弃了,决定回到这里。该网站可能是互联网上少数几个在其规则中声明:“没有问题太微不足道或太“新手””的网站之一(事实上我还没有遇到任何其他网站)。这为我提出当前问题提供了某种基础。

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

预先感谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蛮可爱 2024-08-18 07:31:28

代码行:

Dim fso, MyFile, FileName, TextLine

声明类型为变体的称为“变量”的东西。

变量是内存中的一小块空间,具有名称和类型。您使用它们是为了让程序知道您稍后将在代码中使用它们。

通常,您会为变量指定类型(例如整数或字符串),但编码器没有,因此它默认为变体,它可以采用任何类型(本质上)。

完成后:

Set fso = CreateObject("Scripting.FileSystemObject")

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

Set MyFile = fso.OpenTextFile(FileName, ForReading)

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

因此,您可以使用 myfile 变量对该文件执行进一步的操作。

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

这段代码的想法是逐行读取文件,在遇到每一行时对其内容进行处理。您可以打印它、记录它、向用户显示它等等,如子标题所示!

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

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.

泪是无色的血 2024-08-18 07:31:28

该行所做的就是将它们定义为要在代码中进一步使用的变量

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

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?

仅冇旳回忆 2024-08-18 07:31:28
Dim fso, MyFile, FileName, TextLine

此行定义变量。
这样做的目的是帮助捕获拼写错误,因为变量在整个脚本中被引用。这通常与Option Explicit(通常位于脚本顶部)一起使用。

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

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.

我纯我任性 2024-08-18 07:31:28

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

我希望这有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文