如何在 VBA 中使用 FileSystemObject?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3233203/
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-08 10:37:14  来源:igfitidea点击:

How do I use FileSystemObject in VBA?

excelvbafilesystemobject

提问by l--''''''---------''''''''''''

Is there something that I need to reference? How do I use this:

有什么我需要参考的吗?我如何使用它:

Dim fso As New FileSystemObject
Dim fld As Folder
Dim ts As TextStream

I am getting an error because it does not recognize these objects.

我收到一个错误,因为它无法识别这些对象。

回答by Robert Mearns

Within Excel you need to set a reference to the VB script run-time library. The relevant file is usually located at \Windows\System32\scrrun.dll

在 Excel 中,您需要设置对 VB 脚本运行库的引用。相关文件通常位于\Windows\System32\scrrun.dll

  • To reference this file, load the Visual Basic Editor (ALT+F11)
  • Select Tools > References from the drop-down menu
  • A listbox of available references will be displayed
  • Tick the check-box next to 'Microsoft Scripting Runtime'
  • The full name and path of the scrrun.dllfile will be displayed below the listbox
  • Click on the OKbutton.
  • 要引用此文件,请加载 Visual Basic 编辑器 ( ALT+ F11)
  • 从下拉菜单中选择工具 > 参考
  • 将显示可用参考的列表框
  • 勾选“ Microsoft Scripting Runtime”旁边的复选框
  • 文件的全名和路径scrrun.dll将显示在列表框下方
  • 单击OK按钮。

This can also be done directly in the code if access to the VBA object model has been enabled.

如果已启用对 VBA 对象模型的访问,这也可以直接在代码中完成。

Access can be enabled by ticking the check-box Trust access to the VBA project object modelfound at File > Options > Trust Center > Trust Center Settings > Macro Settings

Access可以通过选中复选框启用Trust access to the VBA project object model在发现文件>选项>信任中心>信任中心设置>宏设置

VBA Macro settings

VBA 宏设置

To add a reference:

添加引用:

Sub Add_Reference()

    Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\scrrun.dll"
'Add a reference

End Sub

To remove a reference:

要删除引用:

Sub Remove_Reference()

Dim oReference As Object

    Set oReference = Application.VBE.ActiveVBProject.References.Item("Scripting")

    Application.VBE.ActiveVBProject.References.Remove oReference
'Remove a reference

End Sub

回答by Stefano Spinucci

In excel 2013 the object creation string is:

在 excel 2013 中,对象创建字符串是:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

instead of the code in the answer above:

而不是上面答案中的代码:

Dim fs,fname
Set fs=Server.CreateObject("Scripting.FileSystemObject")

回答by Gerald Ferreira

These guys have excellent examples of how to use the filesystem object http://www.w3schools.com/asp/asp_ref_filesystem.asp

这些家伙有很好的例子来说明如何使用文件系统对象http://www.w3schools.com/asp/asp_ref_filesystem.asp

<%
dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
%> 

回答by thedanotto

After adding the reference, I had to use

添加引用后,我不得不使用

Dim fso As New Scripting.FileSystemObject

回答by FIRE FOX

After importing the scripting runtime as described above you have to make some slighty modification to get it working in Excel 2010 (my version). Into the following code I've also add the code used to the user to pick a file.

如上所述导入脚本运行时后,您必须稍作修改才能使其在 Excel 2010(我的版本)中运行。在以下代码中,我还添加了用于用户选择文件的代码。

Dim intChoice As Integer
Dim strPath As String

' Select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False

' Show the selection window
intChoice = Application.FileDialog(msoFileDialogOpen).Show

' Get back the user option
If intChoice <> 0 Then
    strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
Else
    Exit Sub
End If

Dim FSO As New Scripting.FileSystemObject
Dim fsoStream As Scripting.TextStream
Dim strLine As String

Set fsoStream = FSO.OpenTextFile(strPath)

Do Until fsoStream.AtEndOfStream = True
    strLine = fsoStream.ReadLine
    ' ... do your work ...
Loop

fsoStream.Close
Set FSO = Nothing

Hope it help!

希望有帮助!

Best regards

此致

Fabio

法比奥