VBA to open file explorer and search for variable in textbox












0















All I want to do is have the user enter a keyword in a text box and when the macro is executed; Open a new Windows' File Explorer window designated by a path



path = C:UsersMEDesktopFolder7


and Search from the variable in SearchBox1 (this is a ActiveX text box on a worksheet.)



mySearch = sht.OLEObjects("SearchBox1").Object.Text & "*" 


I've been looking all over the place and since I am relatively new to VBA Im not sure how to do this.



I saw multiple threads/posts using Shell commands to open a File Explorer Window..



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


When I run the above line an error from the explorer stating,




'Windows cannot find ". Make sure you typed the name correctly, and then try again.'




I need the macro to search for All Files associated with the string (folder names, file names, and words/characters within each type of document(they have all been OCR'd and Indexed by Windows)) It should have the ability to search for incomplete words as well.



I've gotten the Shell to open an explorer window to the path by



Call Shell("explorer.exe " & Chr(34) & "C:UsersMEDesktopFolder7" & Chr(34), vbNormalFocus)


How can the macro search all folders and sub-folders within this freshly opened window? I don't need the results compiled into Excel or any other program. I just need to make a quick search button as if you were to manually open this folder and use the search bar.



Thanks ahead of time for any and all input!










share|improve this question

























  • I may have gotten closer but now am stuck on my Variable within the string.. Call Shell("explorer.exe " & Chr(34) & "search-ms:displayname=Search%20Results%20in%20Folder7&crumb=System.Generic.String%3A(""mySearch"")&crumb=location:C%3A%5CUsers%5CME%5CDesktop%5CFolder7" & Chr(34), vbNormalFocus)

    – Kenny
    Oct 5 '18 at 19:32


















0















All I want to do is have the user enter a keyword in a text box and when the macro is executed; Open a new Windows' File Explorer window designated by a path



path = C:UsersMEDesktopFolder7


and Search from the variable in SearchBox1 (this is a ActiveX text box on a worksheet.)



mySearch = sht.OLEObjects("SearchBox1").Object.Text & "*" 


I've been looking all over the place and since I am relatively new to VBA Im not sure how to do this.



I saw multiple threads/posts using Shell commands to open a File Explorer Window..



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


When I run the above line an error from the explorer stating,




'Windows cannot find ". Make sure you typed the name correctly, and then try again.'




I need the macro to search for All Files associated with the string (folder names, file names, and words/characters within each type of document(they have all been OCR'd and Indexed by Windows)) It should have the ability to search for incomplete words as well.



I've gotten the Shell to open an explorer window to the path by



Call Shell("explorer.exe " & Chr(34) & "C:UsersMEDesktopFolder7" & Chr(34), vbNormalFocus)


How can the macro search all folders and sub-folders within this freshly opened window? I don't need the results compiled into Excel or any other program. I just need to make a quick search button as if you were to manually open this folder and use the search bar.



Thanks ahead of time for any and all input!










share|improve this question

























  • I may have gotten closer but now am stuck on my Variable within the string.. Call Shell("explorer.exe " & Chr(34) & "search-ms:displayname=Search%20Results%20in%20Folder7&crumb=System.Generic.String%3A(""mySearch"")&crumb=location:C%3A%5CUsers%5CME%5CDesktop%5CFolder7" & Chr(34), vbNormalFocus)

    – Kenny
    Oct 5 '18 at 19:32
















0












0








0


2






All I want to do is have the user enter a keyword in a text box and when the macro is executed; Open a new Windows' File Explorer window designated by a path



path = C:UsersMEDesktopFolder7


and Search from the variable in SearchBox1 (this is a ActiveX text box on a worksheet.)



mySearch = sht.OLEObjects("SearchBox1").Object.Text & "*" 


I've been looking all over the place and since I am relatively new to VBA Im not sure how to do this.



I saw multiple threads/posts using Shell commands to open a File Explorer Window..



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


When I run the above line an error from the explorer stating,




'Windows cannot find ". Make sure you typed the name correctly, and then try again.'




I need the macro to search for All Files associated with the string (folder names, file names, and words/characters within each type of document(they have all been OCR'd and Indexed by Windows)) It should have the ability to search for incomplete words as well.



I've gotten the Shell to open an explorer window to the path by



Call Shell("explorer.exe " & Chr(34) & "C:UsersMEDesktopFolder7" & Chr(34), vbNormalFocus)


How can the macro search all folders and sub-folders within this freshly opened window? I don't need the results compiled into Excel or any other program. I just need to make a quick search button as if you were to manually open this folder and use the search bar.



Thanks ahead of time for any and all input!










share|improve this question
















All I want to do is have the user enter a keyword in a text box and when the macro is executed; Open a new Windows' File Explorer window designated by a path



path = C:UsersMEDesktopFolder7


and Search from the variable in SearchBox1 (this is a ActiveX text box on a worksheet.)



mySearch = sht.OLEObjects("SearchBox1").Object.Text & "*" 


I've been looking all over the place and since I am relatively new to VBA Im not sure how to do this.



I saw multiple threads/posts using Shell commands to open a File Explorer Window..



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


When I run the above line an error from the explorer stating,




'Windows cannot find ". Make sure you typed the name correctly, and then try again.'




I need the macro to search for All Files associated with the string (folder names, file names, and words/characters within each type of document(they have all been OCR'd and Indexed by Windows)) It should have the ability to search for incomplete words as well.



I've gotten the Shell to open an explorer window to the path by



Call Shell("explorer.exe " & Chr(34) & "C:UsersMEDesktopFolder7" & Chr(34), vbNormalFocus)


How can the macro search all folders and sub-folders within this freshly opened window? I don't need the results compiled into Excel or any other program. I just need to make a quick search button as if you were to manually open this folder and use the search bar.



Thanks ahead of time for any and all input!







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 5 '18 at 18:46









BigBen

5,7602418




5,7602418










asked Oct 5 '18 at 18:35









KennyKenny

9411




9411













  • I may have gotten closer but now am stuck on my Variable within the string.. Call Shell("explorer.exe " & Chr(34) & "search-ms:displayname=Search%20Results%20in%20Folder7&crumb=System.Generic.String%3A(""mySearch"")&crumb=location:C%3A%5CUsers%5CME%5CDesktop%5CFolder7" & Chr(34), vbNormalFocus)

    – Kenny
    Oct 5 '18 at 19:32





















  • I may have gotten closer but now am stuck on my Variable within the string.. Call Shell("explorer.exe " & Chr(34) & "search-ms:displayname=Search%20Results%20in%20Folder7&crumb=System.Generic.String%3A(""mySearch"")&crumb=location:C%3A%5CUsers%5CME%5CDesktop%5CFolder7" & Chr(34), vbNormalFocus)

    – Kenny
    Oct 5 '18 at 19:32



















I may have gotten closer but now am stuck on my Variable within the string.. Call Shell("explorer.exe " & Chr(34) & "search-ms:displayname=Search%20Results%20in%20Folder7&crumb=System.Generic.String%3A(""mySearch"")&crumb=location:C%3A%5CUsers%5CME%5CDesktop%5CFolder7" & Chr(34), vbNormalFocus)

– Kenny
Oct 5 '18 at 19:32







I may have gotten closer but now am stuck on my Variable within the string.. Call Shell("explorer.exe " & Chr(34) & "search-ms:displayname=Search%20Results%20in%20Folder7&crumb=System.Generic.String%3A(""mySearch"")&crumb=location:C%3A%5CUsers%5CME%5CDesktop%5CFolder7" & Chr(34), vbNormalFocus)

– Kenny
Oct 5 '18 at 19:32














3 Answers
3






active

oldest

votes


















1














Double Click on Cell to search



This is a solution I have combined from various places to open an explorer window at a path, that are filtered (searched) by the term in the selected cell using the windows File Explorer search function. It is triggered by a double click on a cell that contains the search term:



Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.Intersect(Target, Range("A1:AA1048576")) Is Nothing Then
Dim d As String
Dim searchpath As String
Dim searchlocation As String
Cancel = True
d = Selection.Value
'change window name to make sure new explorer window is opened for each instance
'copy string from manual search
searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
'copy string from manual search (e.g. my documents replace USERNAME)
searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
If Not d = "" Then
Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
End If
End If
End Sub


This opens the window in VbNormalFocus, with the window title set to the cell variable (d). The ensures that if this code is run on another cell value a new separate window will be opened. Without this I found the next time I ran the code the explorer window was not updated with the new search value, but just changed focus to the previous result.



edit: "copy from search bar" is the string after location: in the address bar of a manual search in explorer



Using ActiveX Controls



Add an ActiveX Text box (TextBox1) and button (CommandButton1) and add the following codeto the command button:



Private Sub CommandButton1_Click()
Dim d As String
Dim searchpath As String
Dim searchlocation As String
Cancel = True
d = TextBox1.Value
'change window name to make sure new explorer window is opened for each instance
'copy string from manual search
searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
'copy string from manual search (e.g. my documents replace USERNAME)
searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
If Not d = "" Then
Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
End If
End Sub


Now the user can change the text in the text box and clicking the button will open a windows file explorer search of the designated folder in the code.



Screenshot example using button search for "Editable Search Text"






share|improve this answer


























  • thanks for responding. im a little confused. how do I have a activeX txt box or a txt field form control (does it matter which i use?), be the keyword for the search. i can't seam to figure out how to get your code to work.

    – Kenny
    Nov 12 '18 at 16:09






  • 1





    This code was just a snippet from a sheet I had which captured a double click on a cell containing the search value. I'll update my answer with more details and specifically more about having a text box and ActiveX controls...

    – TopCat13
    Nov 13 '18 at 14:13








  • 1





    @Kenny Did this solution work for you?

    – TopCat13
    Nov 21 '18 at 8:04











  • thanks for checking in. Yes, your explanation and pictures helped me understand. Much Appreciated

    – Kenny
    Dec 7 '18 at 10:53



















1














This worked for me:



Sub Tester()
ShowSearch "C:_Stufftest", "*.pdf" 'search by file name
ShowSearch "C:_StuffMail", "reminder", True 'search by keyword
End Sub


Sub ShowSearch(searchWhere, searchFor, Optional SearchByKeyword As Boolean = False)
Const CMD As String = "explorer.exe ""search-ms:crumb=name:{query}&crumb=location:{location}"" "
Dim s
s = Replace(CMD, "{query}", WorksheetFunction.EncodeURL(searchFor))
s = Replace(s, "{location}", WorksheetFunction.EncodeURL(searchWhere))
If SearchByKeyword Then s = Replace(s, "crumb=name:", "crumb=")
'Debug.Print s
Shell s
End Sub


Note: WorksheetFunction.EncodeURL() is 2013 and later. For alternatives see:



How can I URL encode a string in Excel VBA?






share|improve this answer

































    0














    executing Dir() empty after a Dir() with a given path will start to list all files in that dir, you just use if InStr() <> 0 to check against your value.



    sFileName = Dir(path)
    Do While sFileName > ""
    tmp = tmp & sFileName & ";" : sFileName = Dir()
    Loop
    List() = Split(tmp, ";")


    there you have a list of all files inside that path, you can check sub folders the same way by going through each one doing the same thing.






    share|improve this answer
























    • I'm not sure how this will search via my SearchBox1.

      – Kenny
      Oct 5 '18 at 19:03











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52671500%2fvba-to-open-file-explorer-and-search-for-variable-in-textbox%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Double Click on Cell to search



    This is a solution I have combined from various places to open an explorer window at a path, that are filtered (searched) by the term in the selected cell using the windows File Explorer search function. It is triggered by a double click on a cell that contains the search term:



    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("A1:AA1048576")) Is Nothing Then
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = Selection.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End If
    End Sub


    This opens the window in VbNormalFocus, with the window title set to the cell variable (d). The ensures that if this code is run on another cell value a new separate window will be opened. Without this I found the next time I ran the code the explorer window was not updated with the new search value, but just changed focus to the previous result.



    edit: "copy from search bar" is the string after location: in the address bar of a manual search in explorer



    Using ActiveX Controls



    Add an ActiveX Text box (TextBox1) and button (CommandButton1) and add the following codeto the command button:



    Private Sub CommandButton1_Click()
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = TextBox1.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End Sub


    Now the user can change the text in the text box and clicking the button will open a windows file explorer search of the designated folder in the code.



    Screenshot example using button search for "Editable Search Text"






    share|improve this answer


























    • thanks for responding. im a little confused. how do I have a activeX txt box or a txt field form control (does it matter which i use?), be the keyword for the search. i can't seam to figure out how to get your code to work.

      – Kenny
      Nov 12 '18 at 16:09






    • 1





      This code was just a snippet from a sheet I had which captured a double click on a cell containing the search value. I'll update my answer with more details and specifically more about having a text box and ActiveX controls...

      – TopCat13
      Nov 13 '18 at 14:13








    • 1





      @Kenny Did this solution work for you?

      – TopCat13
      Nov 21 '18 at 8:04











    • thanks for checking in. Yes, your explanation and pictures helped me understand. Much Appreciated

      – Kenny
      Dec 7 '18 at 10:53
















    1














    Double Click on Cell to search



    This is a solution I have combined from various places to open an explorer window at a path, that are filtered (searched) by the term in the selected cell using the windows File Explorer search function. It is triggered by a double click on a cell that contains the search term:



    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("A1:AA1048576")) Is Nothing Then
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = Selection.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End If
    End Sub


    This opens the window in VbNormalFocus, with the window title set to the cell variable (d). The ensures that if this code is run on another cell value a new separate window will be opened. Without this I found the next time I ran the code the explorer window was not updated with the new search value, but just changed focus to the previous result.



    edit: "copy from search bar" is the string after location: in the address bar of a manual search in explorer



    Using ActiveX Controls



    Add an ActiveX Text box (TextBox1) and button (CommandButton1) and add the following codeto the command button:



    Private Sub CommandButton1_Click()
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = TextBox1.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End Sub


    Now the user can change the text in the text box and clicking the button will open a windows file explorer search of the designated folder in the code.



    Screenshot example using button search for "Editable Search Text"






    share|improve this answer


























    • thanks for responding. im a little confused. how do I have a activeX txt box or a txt field form control (does it matter which i use?), be the keyword for the search. i can't seam to figure out how to get your code to work.

      – Kenny
      Nov 12 '18 at 16:09






    • 1





      This code was just a snippet from a sheet I had which captured a double click on a cell containing the search value. I'll update my answer with more details and specifically more about having a text box and ActiveX controls...

      – TopCat13
      Nov 13 '18 at 14:13








    • 1





      @Kenny Did this solution work for you?

      – TopCat13
      Nov 21 '18 at 8:04











    • thanks for checking in. Yes, your explanation and pictures helped me understand. Much Appreciated

      – Kenny
      Dec 7 '18 at 10:53














    1












    1








    1







    Double Click on Cell to search



    This is a solution I have combined from various places to open an explorer window at a path, that are filtered (searched) by the term in the selected cell using the windows File Explorer search function. It is triggered by a double click on a cell that contains the search term:



    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("A1:AA1048576")) Is Nothing Then
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = Selection.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End If
    End Sub


    This opens the window in VbNormalFocus, with the window title set to the cell variable (d). The ensures that if this code is run on another cell value a new separate window will be opened. Without this I found the next time I ran the code the explorer window was not updated with the new search value, but just changed focus to the previous result.



    edit: "copy from search bar" is the string after location: in the address bar of a manual search in explorer



    Using ActiveX Controls



    Add an ActiveX Text box (TextBox1) and button (CommandButton1) and add the following codeto the command button:



    Private Sub CommandButton1_Click()
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = TextBox1.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End Sub


    Now the user can change the text in the text box and clicking the button will open a windows file explorer search of the designated folder in the code.



    Screenshot example using button search for "Editable Search Text"






    share|improve this answer















    Double Click on Cell to search



    This is a solution I have combined from various places to open an explorer window at a path, that are filtered (searched) by the term in the selected cell using the windows File Explorer search function. It is triggered by a double click on a cell that contains the search term:



    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("A1:AA1048576")) Is Nothing Then
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = Selection.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End If
    End Sub


    This opens the window in VbNormalFocus, with the window title set to the cell variable (d). The ensures that if this code is run on another cell value a new separate window will be opened. Without this I found the next time I ran the code the explorer window was not updated with the new search value, but just changed focus to the previous result.



    edit: "copy from search bar" is the string after location: in the address bar of a manual search in explorer



    Using ActiveX Controls



    Add an ActiveX Text box (TextBox1) and button (CommandButton1) and add the following codeto the command button:



    Private Sub CommandButton1_Click()
    Dim d As String
    Dim searchpath As String
    Dim searchlocation As String
    Cancel = True
    d = TextBox1.Value
    'change window name to make sure new explorer window is opened for each instance
    'copy string from manual search
    searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
    'copy string from manual search (e.g. my documents replace USERNAME)
    searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
    If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
    'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
    End If
    End Sub


    Now the user can change the text in the text box and clicking the button will open a windows file explorer search of the designated folder in the code.



    Screenshot example using button search for "Editable Search Text"







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 14:54

























    answered Oct 23 '18 at 12:01









    TopCat13TopCat13

    263




    263













    • thanks for responding. im a little confused. how do I have a activeX txt box or a txt field form control (does it matter which i use?), be the keyword for the search. i can't seam to figure out how to get your code to work.

      – Kenny
      Nov 12 '18 at 16:09






    • 1





      This code was just a snippet from a sheet I had which captured a double click on a cell containing the search value. I'll update my answer with more details and specifically more about having a text box and ActiveX controls...

      – TopCat13
      Nov 13 '18 at 14:13








    • 1





      @Kenny Did this solution work for you?

      – TopCat13
      Nov 21 '18 at 8:04











    • thanks for checking in. Yes, your explanation and pictures helped me understand. Much Appreciated

      – Kenny
      Dec 7 '18 at 10:53



















    • thanks for responding. im a little confused. how do I have a activeX txt box or a txt field form control (does it matter which i use?), be the keyword for the search. i can't seam to figure out how to get your code to work.

      – Kenny
      Nov 12 '18 at 16:09






    • 1





      This code was just a snippet from a sheet I had which captured a double click on a cell containing the search value. I'll update my answer with more details and specifically more about having a text box and ActiveX controls...

      – TopCat13
      Nov 13 '18 at 14:13








    • 1





      @Kenny Did this solution work for you?

      – TopCat13
      Nov 21 '18 at 8:04











    • thanks for checking in. Yes, your explanation and pictures helped me understand. Much Appreciated

      – Kenny
      Dec 7 '18 at 10:53

















    thanks for responding. im a little confused. how do I have a activeX txt box or a txt field form control (does it matter which i use?), be the keyword for the search. i can't seam to figure out how to get your code to work.

    – Kenny
    Nov 12 '18 at 16:09





    thanks for responding. im a little confused. how do I have a activeX txt box or a txt field form control (does it matter which i use?), be the keyword for the search. i can't seam to figure out how to get your code to work.

    – Kenny
    Nov 12 '18 at 16:09




    1




    1





    This code was just a snippet from a sheet I had which captured a double click on a cell containing the search value. I'll update my answer with more details and specifically more about having a text box and ActiveX controls...

    – TopCat13
    Nov 13 '18 at 14:13







    This code was just a snippet from a sheet I had which captured a double click on a cell containing the search value. I'll update my answer with more details and specifically more about having a text box and ActiveX controls...

    – TopCat13
    Nov 13 '18 at 14:13






    1




    1





    @Kenny Did this solution work for you?

    – TopCat13
    Nov 21 '18 at 8:04





    @Kenny Did this solution work for you?

    – TopCat13
    Nov 21 '18 at 8:04













    thanks for checking in. Yes, your explanation and pictures helped me understand. Much Appreciated

    – Kenny
    Dec 7 '18 at 10:53





    thanks for checking in. Yes, your explanation and pictures helped me understand. Much Appreciated

    – Kenny
    Dec 7 '18 at 10:53













    1














    This worked for me:



    Sub Tester()
    ShowSearch "C:_Stufftest", "*.pdf" 'search by file name
    ShowSearch "C:_StuffMail", "reminder", True 'search by keyword
    End Sub


    Sub ShowSearch(searchWhere, searchFor, Optional SearchByKeyword As Boolean = False)
    Const CMD As String = "explorer.exe ""search-ms:crumb=name:{query}&crumb=location:{location}"" "
    Dim s
    s = Replace(CMD, "{query}", WorksheetFunction.EncodeURL(searchFor))
    s = Replace(s, "{location}", WorksheetFunction.EncodeURL(searchWhere))
    If SearchByKeyword Then s = Replace(s, "crumb=name:", "crumb=")
    'Debug.Print s
    Shell s
    End Sub


    Note: WorksheetFunction.EncodeURL() is 2013 and later. For alternatives see:



    How can I URL encode a string in Excel VBA?






    share|improve this answer






























      1














      This worked for me:



      Sub Tester()
      ShowSearch "C:_Stufftest", "*.pdf" 'search by file name
      ShowSearch "C:_StuffMail", "reminder", True 'search by keyword
      End Sub


      Sub ShowSearch(searchWhere, searchFor, Optional SearchByKeyword As Boolean = False)
      Const CMD As String = "explorer.exe ""search-ms:crumb=name:{query}&crumb=location:{location}"" "
      Dim s
      s = Replace(CMD, "{query}", WorksheetFunction.EncodeURL(searchFor))
      s = Replace(s, "{location}", WorksheetFunction.EncodeURL(searchWhere))
      If SearchByKeyword Then s = Replace(s, "crumb=name:", "crumb=")
      'Debug.Print s
      Shell s
      End Sub


      Note: WorksheetFunction.EncodeURL() is 2013 and later. For alternatives see:



      How can I URL encode a string in Excel VBA?






      share|improve this answer




























        1












        1








        1







        This worked for me:



        Sub Tester()
        ShowSearch "C:_Stufftest", "*.pdf" 'search by file name
        ShowSearch "C:_StuffMail", "reminder", True 'search by keyword
        End Sub


        Sub ShowSearch(searchWhere, searchFor, Optional SearchByKeyword As Boolean = False)
        Const CMD As String = "explorer.exe ""search-ms:crumb=name:{query}&crumb=location:{location}"" "
        Dim s
        s = Replace(CMD, "{query}", WorksheetFunction.EncodeURL(searchFor))
        s = Replace(s, "{location}", WorksheetFunction.EncodeURL(searchWhere))
        If SearchByKeyword Then s = Replace(s, "crumb=name:", "crumb=")
        'Debug.Print s
        Shell s
        End Sub


        Note: WorksheetFunction.EncodeURL() is 2013 and later. For alternatives see:



        How can I URL encode a string in Excel VBA?






        share|improve this answer















        This worked for me:



        Sub Tester()
        ShowSearch "C:_Stufftest", "*.pdf" 'search by file name
        ShowSearch "C:_StuffMail", "reminder", True 'search by keyword
        End Sub


        Sub ShowSearch(searchWhere, searchFor, Optional SearchByKeyword As Boolean = False)
        Const CMD As String = "explorer.exe ""search-ms:crumb=name:{query}&crumb=location:{location}"" "
        Dim s
        s = Replace(CMD, "{query}", WorksheetFunction.EncodeURL(searchFor))
        s = Replace(s, "{location}", WorksheetFunction.EncodeURL(searchWhere))
        If SearchByKeyword Then s = Replace(s, "crumb=name:", "crumb=")
        'Debug.Print s
        Shell s
        End Sub


        Note: WorksheetFunction.EncodeURL() is 2013 and later. For alternatives see:



        How can I URL encode a string in Excel VBA?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 6 '18 at 17:56

























        answered Oct 5 '18 at 20:51









        Tim WilliamsTim Williams

        85.6k96885




        85.6k96885























            0














            executing Dir() empty after a Dir() with a given path will start to list all files in that dir, you just use if InStr() <> 0 to check against your value.



            sFileName = Dir(path)
            Do While sFileName > ""
            tmp = tmp & sFileName & ";" : sFileName = Dir()
            Loop
            List() = Split(tmp, ";")


            there you have a list of all files inside that path, you can check sub folders the same way by going through each one doing the same thing.






            share|improve this answer
























            • I'm not sure how this will search via my SearchBox1.

              – Kenny
              Oct 5 '18 at 19:03
















            0














            executing Dir() empty after a Dir() with a given path will start to list all files in that dir, you just use if InStr() <> 0 to check against your value.



            sFileName = Dir(path)
            Do While sFileName > ""
            tmp = tmp & sFileName & ";" : sFileName = Dir()
            Loop
            List() = Split(tmp, ";")


            there you have a list of all files inside that path, you can check sub folders the same way by going through each one doing the same thing.






            share|improve this answer
























            • I'm not sure how this will search via my SearchBox1.

              – Kenny
              Oct 5 '18 at 19:03














            0












            0








            0







            executing Dir() empty after a Dir() with a given path will start to list all files in that dir, you just use if InStr() <> 0 to check against your value.



            sFileName = Dir(path)
            Do While sFileName > ""
            tmp = tmp & sFileName & ";" : sFileName = Dir()
            Loop
            List() = Split(tmp, ";")


            there you have a list of all files inside that path, you can check sub folders the same way by going through each one doing the same thing.






            share|improve this answer













            executing Dir() empty after a Dir() with a given path will start to list all files in that dir, you just use if InStr() <> 0 to check against your value.



            sFileName = Dir(path)
            Do While sFileName > ""
            tmp = tmp & sFileName & ";" : sFileName = Dir()
            Loop
            List() = Split(tmp, ";")


            there you have a list of all files inside that path, you can check sub folders the same way by going through each one doing the same thing.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 5 '18 at 18:53









            Sebastian BlumenbergSebastian Blumenberg

            243




            243













            • I'm not sure how this will search via my SearchBox1.

              – Kenny
              Oct 5 '18 at 19:03



















            • I'm not sure how this will search via my SearchBox1.

              – Kenny
              Oct 5 '18 at 19:03

















            I'm not sure how this will search via my SearchBox1.

            – Kenny
            Oct 5 '18 at 19:03





            I'm not sure how this will search via my SearchBox1.

            – Kenny
            Oct 5 '18 at 19:03


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52671500%2fvba-to-open-file-explorer-and-search-for-variable-in-textbox%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini