Home > Enterprise >  Replace Hyperlinks to Local Directory
Replace Hyperlinks to Local Directory

Time:03-21

I'm trying the following: when VBA finds hyperlink with the word "servlet" in its address, hyperlink name is being searched in the defined directory, and when it founds a matching file, links it then to the file:

Sub Replace_Link()
Dim strPath As String
Dim oRng As Range
Dim sName As String
Dim H As Hyperlink
    strPath = ActiveDocument.Path & "\attachments\"
    For Each H In ActiveDocument.Hyperlinks
        If InStr(H.Address, "servlet") <> 0 Then
            Set oRng = H.Range.Select
            sName = Dir$(strPath & Trim(oRng.Text) & ".*")
            If Not sName = "" Then
                oRng.Hyperlinks.Add Anchor:=oRng, Address:=strPath & sName, TextToDisplay:=Trim(Rong.Text)
            Set oRng = Nothing
        End If
    Next H
End Sub 

E.g.: I have a hyperlink image.png, VBA founds in the folder file image and links the file.

This code throws an error in the Set Rng = H.Range.Select row, that expected function or variable. Why can't I define the selection with variable? If I write Selection instead of Rng, errors are being thrown elswhere.

CodePudding user response:

You can only set a range to a method if that method is a function that returns a range. Select doesn't return anything, it simply moves the cursor.

All you need is Set Rng = H.Range

  • Related