什么是"Dim fso,MyFile,FileName,TextLine"在VBA中? [英] What is "Dim fso, MyFile, FileName, TextLine" in VBA?

查看:282
本文介绍了什么是"Dim fso,MyFile,FileName,TextLine"在VBA中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

什么是"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?

我已阅读以下内容: http://msdn.microsoft.com/en-us/library/h7se9d4f(VS.85).aspx

I've read this: http://msdn.microsoft.com/en-us/library/h7se9d4f(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.

因此,请任何人简单地向我解释"fso"是什么.确切地说,上面代码的第三行是关于所有内容的.

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

提前谢谢大家.

推荐答案

代码行:

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.

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

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).

完成后:

Set fso = CreateObject("Scripting.FileSystemObject")

然后fso包含一些可以对文件系统进行处理的代码.

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

Set MyFile = fso.OpenTextFile(FileName, ForReading)

意味着您正在使用fso功能打开在"filename"变量中指定的文件名,并且已在"myfile"变量中添加了对该文件的引用.

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.

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

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

"do while"循环一次读取该文件一行(myfile.readline),并将结果放入"textline"变量中,该变量在您每次遍历该文件时都保留与文件不同的文本行循环,直到文件完成.

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!

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

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.

这篇关于什么是"Dim fso,MyFile,FileName,TextLine"在VBA中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆