【问题标题】:How do I use FileSystemObject in VBA?如何在 VBA 中使用 FileSystemObject?
【发布时间】:2011-03-15 01:49:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: excel vba filesystemobject


    【解决方案1】:

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

    • 要引用此文件,请加载 Visual Basic 编辑器 (ALT+F11)
    • 从下拉菜单中选择工具 > 参考文献
    • 将显示可用参考的列表框
    • 勾选“Microsoft Scripting Runtime”旁边的复选框
    • scrrun.dll文件的全名和路径会显示在列表框下方
    • 点击确定按钮。

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

    可以通过勾选文件>选项>信任中心>信任中心设置>宏设置中的复选框Trust access to the VBA project object model来启用访问

    添加参考:

    Sub Add_Reference()
    
        Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\scrrun.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
    

    【讨论】:

    • 难道没有办法通过命令行来做到这一点,比如包含一个库或其他东西吗?
    • 有没有办法自动勾选“Microsoft Scripting Runtime”复选框?适用于所有 Excel 文件?我正在使用 Excel 2016
    • 查看此问题的已接受答案。 stackoverflow.com/questions/9879825/…
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 更明确一点,我通常将其视为Dim fso As Object
    【解决方案3】:

    这些家伙有很好的例子来说明如何使用文件系统对象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
    %> 
    

    【讨论】:

    • 这段代码 sn-p 演示了在 ASP/IIS not excel 中使用 FSO
    【解决方案4】:

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

    Dim fso As New Scripting.FileSystemObject
    

    【讨论】:

      【解决方案5】:

      如上所述导入脚本运行时后,您必须稍作修改才能使其在 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
      

      希望对您有所帮助!

      最好的问候

      法比奥

      【讨论】:

        猜你喜欢
        • 2015-10-12
        • 2013-10-29
        • 2022-12-19
        • 2016-02-14
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多