jinsungy
jinsungy

Reputation: 10835

How to programmatically edit all hyperlinks in a Word document?

Is there a macro, VBA code or VBScript that I can write to edit the urls of all the hyperlinks in my Word document? Either Word 97-2003 or docx format.

Upvotes: 14

Views: 25312

Answers (3)

Chema
Chema

Reputation: 745

Thanks to Tester for the solution, used it for a quick replace:

Sub ReplaceLinks()

Dim doc As Document
Dim link, i

'Loop through all open documents.
For Each doc In Application.Documents
    'Loop through all hyperlinks.
    For i = 1 To doc.Hyperlinks.Count

        'Update old bookmarks to https
        doc.Hyperlinks(i).Address = Replace(doc.Hyperlinks(i).Address, "gopher:", "https://")

   Next
Next
End Sub

Upvotes: 0

Alexandra Robin
Alexandra Robin

Reputation: 1

This helped me immensely. User had opened Word Docs containing hyperlinks through her mapped drive instead of going in the long way through network. Hundreds of docs will be saved!

I used mid() function:

Sub FixMyHyperlink()

    Dim doc As Document
    Dim link, i

    'Loop through all open documents.
    For Each doc In Application.Documents
        'Loop through all hyperlinks.
        For i = 1 To doc.Hyperlinks.Count
            'If the hyperlink matches.
            If LCase(doc.Hyperlinks(i).Address) Like "*partOfHyperlinkHere*" Then
                'Change the links address. Used wildcards (*) on either side.
                doc.Hyperlinks(i).Address = Mid(doc.Hyperlinks(i).Address, 70,20)        '
                'Change the links display text if desired.
                'doc.Hyperlinks(i).TextToDisplay = "Oatmeal Chocolate Chip Cookies"
            End If
        Next
    Next
End Sub

Upvotes: 0

Tester101
Tester101

Reputation: 8172

Dim doc As Document
Dim link, i
'Loop through all open documents.
For Each doc In Application.Documents
    'Loop through all hyperlinks.
    For i = 1 To doc.Hyperlinks.Count
        'If the hyperlink matches.
        If LCase(doc.Hyperlinks(i).Address) = "http://www.yahoo.com/" Then
            'Change the links address.
            doc.Hyperlinks(i).Address = "http://www.google.com/"
            'Change the links display text if desired.
            doc.Hyperlinks(i).TextToDisplay = "Changed to Google"
        End If
    Next
Next

Here is a link to all the Hyperlink Methods and Properties

Upvotes: 14

Related Questions