ievakr
03-18-2022, 06:00 AM
Hello,
What I'm trying to do is to find text, that matches the file name in a path and creates a hyperlink for it.
Now I have code, that this should be done only for the selected text, but still, it is not successful.
What I want to do e.g.: I have text Gantt.gif. It matches with the same file name in the Attachments folder and adds a hyperlink to it.
Sub HLink_Selected_Text()
Dim strPath As String
Dim oRng As Range
Dim sName As String
strPath = "..\attachments\"
Set oRng = Selection.Range
sName = Dir$(strPath & Trim(oRng.Text) & ".*")
If Not sName = "" Then
oRng.Hyperlinks.Add Anchor:=oRng, Address:=strPath & sName, TextToDisplay:=Trim(oRng.Text)
Else
Beep
MsgBox "Matching file not found"
End If
Set oRng = Nothing
End Sub
What I'm trying to do is to find text, that matches the file name in a path and creates a hyperlink for it.
Now I have code, that this should be done only for the selected text, but still, it is not successful.
What I want to do e.g.: I have text Gantt.gif. It matches with the same file name in the Attachments folder and adds a hyperlink to it.
Sub HLink_Selected_Text()
Dim strPath As String
Dim oRng As Range
Dim sName As String
strPath = "..\attachments\"
Set oRng = Selection.Range
sName = Dir$(strPath & Trim(oRng.Text) & ".*")
If Not sName = "" Then
oRng.Hyperlinks.Add Anchor:=oRng, Address:=strPath & sName, TextToDisplay:=Trim(oRng.Text)
Else
Beep
MsgBox "Matching file not found"
End If
Set oRng = Nothing
End Sub