如何在 VBA 中使用 FileSystemObject? [英] How do I use FileSystemObject in VBA?

查看:37
本文介绍了如何在 VBA 中使用 FileSystemObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

  • 要引用此文件,请加载Visual Basic 编辑器 (ALT+F11)
  • 从下拉菜单中选择工具 > 参考
  • 将显示可用参考的列表框
  • 勾选Microsoft Scripting Runtime"旁边的复选框
  • 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.dll file will be displayed below the listbox
  • Click on the OK button.

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

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 model found at File > Options > Trust Center > Trust Center Settings > Macro Settings

添加引用:

Sub Add_Reference()

    Application.VBE.ActiveVBProject.References.AddFromFile "C:WindowsSystem32scrrun.dll"
'Add a reference

End Sub

删除引用:

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

这篇关于如何在 VBA 中使用 FileSystemObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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