Share via

hyperlink replace not working

Anonymous
2012-10-20T07:58:43+00:00

I have used Alt&F9 to view the hyperlink field codes, then Ctrl&H to replace part of the hyperlink address. It looks OK, but when I toggle Alt&F9 back to show the underlined hyperlink and go into Edit Hyperlink, the change has not taken place. If I make the same change to a single hyperlink in Edit Hyperlink, the change is OK. I need to change hundreds of hyperlinks in a document, so really need to get Find/Replace to work correctly. I am using Word 2007 in Windows Vista Home Premium. Any ideas please?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-10-20T10:17:03+00:00

Press Alt+F11 to activate the Visual Basic Editor.

Select Insert > Module to create a "module" where you can insert code.

Copy the code (from Sub ... up to and including End Sub) from my reply.

Paste it into the module window.

Change FindText to the text to want to search for.

Change ReplaceText to the text you want to replace it with.

With the insertion point anywhere between Sub and End Sub, press F5 to run the macro.

Press Alt+F11 to return to Word and check the result.

If it has worked OK, you can remove the code:

Press Alt+F11 again to activate the Visual Basic Editor.

Select File > Remove Module1...

Click No in the dialog that asks whether you want to export the module.

Press Alt+F4 to close the Visual Basic Editor.

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-10-20T10:34:18+00:00

    Worked like a charm, and I have learnt something new too. Many thanks.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-10-20T09:58:30+00:00

    Thank you. I am ashamed to say that I don't know how to run a macro in Word. Please advise and then I will give it a go.

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2012-10-20T09:49:03+00:00

    You could run this small macro:

    Sub ReplaceInHyperlinks()

        Dim hyp As Hyperlink

        For Each hyp In ActiveDocument.Hyperlinks

            hyp.Address = Replace(hyp.Address, "FindText", "ReplaceText")

        Next hyp

    End Sub

    Substitute the appropriate text for "FindText" and "ReplaceText".

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2012-10-20T09:39:00+00:00

    I have used Alt&F9 to view the hyperlink field codes, then Ctrl&H to replace part of the hyperlink address. It looks OK, but when I toggle Alt&F9 back to show the underlined hyperlink and go into Edit Hyperlink, the change has not taken place. If I make the same change to a single hyperlink in Edit Hyperlink, the change is OK. I need to change hundreds of hyperlinks in a document, so really need to get Find/Replace to work correctly. I am using Word 2007 in Windows Vista Home Premium. Any ideas please?

    Possible the following code will allow you to do what you want

    ' Macro created 26/10/01 by Doug Robbins to update links in a document

    '

    Dim alink As Field, linktype As Range, linkfile As Range

    Dim linklocation As Range, i As Integer, j As Integer, linkcode As Range

    Dim Message, Title, Default, Newfile

    Dim counter As Integer

    counter = 0

    For Each alink In ActiveDocument.Fields

        If alink.Type = wdFieldLink Then 

            Set linkcode = alink.Code

            i = InStr(linkcode, Chr(34))

            Set linktype = alink.Code

            linktype.End = linktype.Start + i

            j = InStr(Mid(linkcode, i + 1), Chr(34))

            Set linklocation = alink.Code

            linklocation.Start = linklocation.Start + i + j - 1

            If counter = 0 Then

                Set linkfile = alink.Code

                linkfile.End = linkfile.Start + i + j - 1

                linkfile.Start = linkfile.Start + i

                Message = "Enter the modified path and filename following this Format " & linkfile

                Title = "Update Link"

                Default = linkfile

                Newfile = InputBox(Message, Title, Default)

            End If

            linkcode.Text = linktype & Newfile & linklocation

            counter = counter + 1

        End If

    Next alink

    Was this answer helpful?

    0 comments No comments