from VBA, run a search in explorer within a design

2019-09-10 11:02发布

问题:

I'm relatively new to VBA and working on something for work that will launch a given folder in explorer, run a search in explorer for files in that folder whose names contain a certain string, and show me the results in explorer. I've been using shell, and was able to individually open a specific folder, and run a search...but I can't figure out how to do both tasks simultaneously.

Here's some different things I've tried: nothing has worked.

Sub search_Files()

    Dim folderName As String

    folderName = "\\Users\itsMe\Documents"

    Call Shell("C:\WINDOWS\explorer.exe ""  "" & folderName _
                  &"" ""&search-ms://query=h&", vbNormalFocus)

    Call Shell("explorer.exe "" ""search-ms:query=h&crumb=location:\\Users\itsMe\Documents", vbNormalFocus)

    Call Shell("C:\WINDOWS\explorer.exe"" & FolderName &", vbNormalFocus)

    RetVal = Shell( _
    "c:\Windows\explorer.exe ""search-ms:displayname=Search%20Results&crumb=\\Users\itsMe\Documents" _
    & h & "%20kind%3A%3Dfolder&crumb=location:" _
    & folderName, vbNormalFocus)

End Sub

Could someone please provide me with the proper code on how to do this? (I want to use shell.)

回答1:

Try this - it worked for me...

Call Shell("explorer.exe " & Chr(34) & "search-ms:query=*.pdf&crumb=location:c:\ad\" & Chr(34), vbNormalFocus)


回答2:

Do you mean your code need to replace the fifth one as below? I am tried to modify the related destination folder already. But still fail. Not sure I did wrong in which step.

Sub search_Files()

    Dim folderName As String

    folderName = "C:\Users\lkam\Documents"

    Call Shell("C:\WINDOWS\explorer.exe "" "" & folderName _
                  &"" ""&search-ms://query=h&", vbNormalFocus)

    Call Shell("explorer.exe " & Chr(34) & "search-ms:query=*.pdf&crumb=location:c:\ad\" & Chr(34), vbNormalFocus)

    Call Shell("C:\WINDOWS\explorer.exe"" & FolderName &", vbNormalFocus)

    RetVal = Shell( _
    "c:\Windows\explorer.exe ""search-ms:displayname=Search%20Results&crumb=C:\Users\lkam\Documents" _
    & h & "%20kind%3A%3Dfolder&crumb=location:" _
    & folderName, vbNormalFocus)

End Sub