调用 Shell - 在 VBA 中搜索资源管理器 - 解决方法

Calling Shell - explorer search in VBA - Workaround

我在 VBA 中有一个代码,它将使用给定的搜索参数调用资源管理器,并将在资源管理器中的特定位置查找并列出具有给定名称的文件。

代码:

RetVal = Shell( _
"c:\Windows\explorer.exe ""search-ms:displayname=Search%20Results&crumb=System.Generic.String%3A~%3D" _
& filename & "%20kind%3A%3Dfolder&crumb=location:" _
& location, vbNormalFocus)

我在英语上使用它时效果很好windows。

有什么方法可以改进此代码,使其可以在其他语言平台上运行,或者至少有一个变通方法可以使其在德语平台上运行 Windows?

编辑:

澄清我需要发生的事情:

工作簿中有不同名称的超链接(例如 "banana"),当用户打开名为 "banana" 的超链接时,脚本会调用 shell,打开资源管理器window 并列出已定义文件夹中包含单词 "banana" 的所有文件(这些文件不是像 .xls 这样的 excel 文件)。

由于其他语言 Windows 浏览器使用不同的搜索命令,它仅适用于英语 Windows。

德语 Windows 的搜索结果如下所示:

search-ms:displayname=Suchergebnisse%20in%20"SYS%20(C%3A)"&crumb=System.Generic.String%3A & filename & &crumb=location: & location

如果您想自己尝试,请注意该位置也应如下所示:C%3A%5C for C:\ 才能正常工作。

编辑2:

所以我已经弄清楚问题出在哪里了。 %20kind%3A%3Dfolder 部分在德语 Windows 中有所不同,所以当我摆脱它时,它开始在两个平台上工作。

这是工作版本:

RetVal = Shell("c:\Windows\explorer.exe ""search-ms:displayname=backup%20for:%20" & _
backup & "&crumb=System.Generic.String%3A~%3D" & backup & _
"%20&crumb=location:" & location, vbNormalFocus)

我仍在寻找一种无需使用 Shell 即可获得相同结果的方法。

为什么不避免一起使用 shell 而使用类似这样的东西,

        Dim myFolder As String
        Dim myFile As String
        Dim wb as Workbook
        Dim i As Long, tempLimit As Long
        Application.FileDialog(msoFileDialogFolderPicker).Title = "Select Folder Containing Excel Files"
        myFolder = Application.FileDialog(msoFileDialogFolderPicker)
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then
                myFolder = .SelectedItems(1)
            End If
        End With
        myFile = Dir(myFolder & "\*.xls*")
        Do While myFile <> ""
            Set wb = Application.Workbooks.Open(myFolder & "\" & myFile)
            ...
        Loop

@Jeanno 确定了一种获取所有匹配 "banana" 文件的方法。这只是调整 his/her 代码以获取所有 Word 文档(但您可以看到它也会 find.txt、.html 或 .)。另一种方法是使用 FileSystemObject (FSO) 来完成同样的事情。您也可以使用 wscript (Windows Script Host) 来获取信息,即使它会使事情变得过于复杂。有很多方法可以给这只猫剥皮。 :)

    Dim myFolder As String
    Dim myFile As String
    Dim wb As Workbook
    Dim i As Long, tempLimit As Long
    Dim r As Integer

    r = 1
    Application.FileDialog(msoFileDialogFolderPicker).Title = "Select Folder Containing .doc Files"
    myFolder = Application.FileDialog(msoFileDialogFolderPicker)
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            myFolder = .SelectedItems(1)
        End If
    End With
    myFile = Dir(myFolder & "\*.doc*")
    Do While myFile <> ""
        'Do what you need to do with the found files
        Sheet1.Cells(r, 1).Value = myFile

        'Call Dir again without arguments to loop through all files that matched our criteria
        myFile = Dir
        r = r + 1
    Loop

展示 FSO 的第二个答案

Sub GetFiles()

Dim FSO as Object 'Late binding <- Prefered method
'or As New FileSystemObject  for Early Binding 
'but don't forget to add a reference to Scripting Runtime!
Dim oFile as Object
Dim oFolder as Object
Dim sFileSpec as String
Dim r As Integer

r = 1
sFileSpec = "banana"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder("C:\path\to\Predefined\Folder\") 
'Or use the folder picker method already used

For Each oFile In oFolder.Files
    If Instr(1, oFile.Name, sFileSpec, vbTextCompare) Then
        With Worksheets("Sheet1")
            .Cells(r, 1) = "File Name:"
            .Cells(r, 3) = "File Size:"
            .Cells(r, 5) = "Date:"
            .Cells(r, 2) = oFile.Name
            .Cells(r, 4) = oFile.Size
            .Cells(r, 5) = oFile.DateLastModified
        End With
        r = r + 1
    End If
Next oFile

End Sub