簡體   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