I grabbed someone else's code and tried multiple different functions to make it fit my needs.
I use a Word document for PLC reports. I'm trying to take selected text (like a station number, BM150 for example), and find a file from the partial text within any subfolder from the designated path and then hyperlink to it.
Sub HLink_Selected_Text()
Dim strPath As String
Dim StrSelection As Range
Dim sName As String
Dim fs As String
strPath = "filepath" 'the path to search
Set StrSelection = Selection.Range
sName = Dir$(strPath & Trim(StrSelection.Text) & ".*") 'change extension to ".*") for any file
fs = strPath & sName
If Not sName = "" Then
StrSelection.Hyperlinks.Add Anchor:=StrSelection, Address:=fs, TextToDisplay:=Trim(StrSelection.Text)
Else
MsgBox "Matching document not found"
End If
End Sub
This works if I type the exact file name and exact file path but I only input part of the file name in the report, and I want it to search multiple subfolders.