David PW Coates
David PW Coates

Reputation: 13

Update part of a hyperlink in Word

We have migrated a server and transferred the files over using the same share path. My customer has got a word document with hyperlinks in it which point to the older server name.

i.e. \\serverOld\accounts\1234.pdf and \\serverNew\accounts\1234.pdf

I have found this VB Script below that has done what i need but it is for Excel not Word.

Sub HyperLinkChange()
   Dim oldtext As String
   Dim newtext As String
   Dim h As Hyperlink

' These can be any text portion of a hyperlink, such as ".com" or ".org".
       oldtext = "\\topscan-server" 
       newtext = "\\ts-sbs" 

' Check all hyperlinks on active sheet.
       For Each h In ActiveSheet.Hyperlinks
       x = InStr(1, h.Address, oldtext)
       If x > 0 Then
           If h.TextToDisplay = h.Address Then
                h.TextToDisplay = newtext
           End If
           h.Address = Application.WorksheetFunction. _
           Substitute(h.Address, oldtext, newtext)
       End If
       Next
End Sub

Please can someone help me edit this text to work with Microsoft Word 2010?

Upvotes: 1

Views: 4196

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149277

Try this

Sub HyperLinkChange()
   Dim oldtext As String, newtext As String
   Dim h As Hyperlink

    oldtext = "\\topscan-server"
    newtext = "\\ts-sbs"

    For Each h In ActiveDocument.Hyperlinks
       If InStr(1, h.Address, oldtext) Then
           If h.TextToDisplay = h.Address Then
                h.TextToDisplay = newtext
           End If
           h.Address = Replace(h.Address, oldtext, newtext)
       End If
    Next
End Sub

Upvotes: 4

Related Questions