from VBA, run a search in explorer within a designated folder

試著忘記壹切 提交于 2019-12-12 03:03:35

问题


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


来源:https://stackoverflow.com/questions/38004097/from-vba-run-a-search-in-explorer-within-a-designated-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!