繁体   English   中英

VBA 运行时错误 445 - Application.FileSearch

[英]VBA RunTime Error 445 - Application.FileSearch

我和这个链接上的朋友有同样的问题: http ://www.vbforums.com/showthread.php?503199-RESOLVED-Opening-an-excel-file-in-VB-without-the-exact-name&highlight= 打开%20file%20excel

基本上我想打开一个文件,我只知道文件名的一部分,使用 VBA 编码。

我在上面的网站上找到了潜在的解决方案,但不幸的是,我的编译器给我运行时错误 445

Sub openfile()
    Dim i As Integer
    With Application.FileSearch
        'Change the path to your path
        .LookIn = "C:\Temp"
        '* represents wildcard characters
        .FileName = "Sales_Report_1_4_2008*.xls"
        If .Execute > 0 Then 'Workbook exists
            'open all files that find the match
            For i = 1 To .FoundFiles.Count
                Workbooks.Open (.FoundFiles(i))
            Next i
        End If
    End With
End Sub

谁能帮我让这段代码在 Excek 2016 上运行?

非常感谢你们

我认为FileSearch已停产。 可以使用文件系统对象。 可能添加对“ Microsoft脚本运行时”的引用,然后尝试

Sub openfile()
    Dim Path As String
    Dim FSO As FileSystemObject
    Dim Fl  As File
    Dim Fld As Folder

    Path = "C:\temp\"
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Fld = oFSO.GetFolder(strPath)

    For Each Fl In Fld.Files
      If Ucase(Fl.Name) Like Ucase("Sales_Report_1_4_2008*.xls") Then
        Workbooks.Open (Fl.Path)
      End If
    Next Fl

    Set FSO = Nothing
    Set Fl = Nothing
    Set Fld = Nothing
End Sub

甚至使用Dir函数进行更简单的循环

    Sub openfile()
        Dim Path As String
        Dim Fname As String

        Path = "C:\temp\"
        Fname = Dir(Path & "Sales_Report_1_4_2008*.xls")
        Do While Fname <> ""
           Workbooks.Open (Path & Fname)
        Fname = Dir
        Loop

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM