Search Files by Tags

Alexasaurus

New Member
Joined
Mar 26, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am very new to VBA and have been learning as I go on a work project. One of the things that was requested was to have a search bar to find other files of a similar problem solving event.
What I want to be able to do to have a vba program that opens file explorer and searches by tags. but all I have been able to manage to figure out is how to open the file explorer using Shell. I have searched high and low on the internet trying to find anything that will specify searching by tags.

If I use the search bar on the file explorer I can search by tags by typing "tags:[tag name]"

I tried something like

Sub SearchTool()
Dim searchlocation As String
Dim tagname As String
Dim searchtag As String

searchlocation = \\my files
tagname = sheet1.Range("A1").Value
searchtag="tags:"& tagname

Call Shell ("explorer.exe" & " " & searchlocation & "search-ms:query = searchtag", vbNormalFocus)

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I have searched high and low on the internet trying to find anything that will specify searching by tags.
... and found nothing, or did but could not make it work? By searching
vba file search using tags

I got these, among others.
Have to admit that I've never done this so all I can do is research. I don't think I've ever saved a file with tags.
 
Upvote 0
I actually just found something and was able to modify it and "Frankenstein" some other code together to get it to search by tags. For some reason, it will only work properly if there isn't already a file explorer open so thats why the "CloseWindow" sub is there. Not very pretty but does the job.

VBA Code:
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

Sub SearchEngine()
Dim filesearch As String
Dim tagname As String
Dim tagsearch As String

filesearch = "put your file location here"
tagname = Sheet1.Range("A1").Text 
tagsearch = "tags:" & Chr(34) & tagname & Chr(34)

Call CloseWindow
ShowSearch filesearch, tagsearch, True


End Sub

Sub CloseWindow()

    Dim objWMIcimv2 As Object, objProcess As Object, objList As Object
    Dim intError As Integer

    Set objWMIcimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='explorer.exe'")

    For Each objProcess In objList
        intError = objProcess.Terminate
        If intError <> 0 Then Exit For
    Next

    Set objWMIcimv2 = Nothing
    Set objList = Nothing
    Set objProcess = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top