Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Hyperlinks in Word 1

Status
Not open for further replies.

Bill4tektips

Technical User
Aug 5, 2005
175
GB
I have a word document with about 700 hyperlinks to various documents within a WSS site and now they have changed the address of the WSS site.
I have tried doing a "Show Field Codes" and doing a Find and Replace but when I undo Show Field Codes and hover over the links they are still showing the old address but the field codes show the new address. I am trying to change from to Can anyone help?
 
Well, just go through, change each one, link by link, and take a coffee break between each change. Will that work? [wink]

Okay... up front, the below is done in VBA.. if you are uncomfortable with it, and need more help than what I put here, post back with more questions, and I or someone else can guide you through this - this one should be simple enough to knock out with VBA in a matter of seconds.

Try something like this (just tested to look at - using VBA):
Code:
Sub TestSearchLink()
    Dim x As Hyperlink
    MsgBox ThisDocument.Hyperlinks.Count
    For Each x In ThisDocument.Hyperlinks
        If InStr(x.Address, "google") Then MsgBox "Howdy!"
    Next x
End Sub

That's just to search through it, so to change it, you could use Replace in there... so...
Code:
Sub TestChangeLink()
    Dim x As Hyperlink
    MsgBox ThisDocument.Hyperlinks.Count
    For Each x In ThisDocument.Hyperlinks
        If InStr(x.Address, "google") Then MsgBox "Howdy!"
        x.Address = Replace(x.Address, "google", "msn")
        MsgBox x.Address
    Next x
End Sub


To test the above, with a blank Word Document, I added "Test Search My Link", and then linked that text to
The code you have to put in the particular Word Doc in order to use the "ThisDocument" object. You could use a more general document object if you want to put it in Normal.dot or Normal.dotx.

So give it a whirl, change to suit your needs, and post back with your progress.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
After doing the Find/Replace, you could run the following macro:
Code:
Sub UpdateHyperlinkDisplay()
Dim HLnk As Hyperlink
For Each HLnk In ActiveDocument.Hyperlinks
  HLnk.TextToDisplay = HLnk.Address
Next
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
kjv1611, Thanks for that but when I try it it just comes up with a Microsoft Word box with "O" in it. Even when I try a Test document linked to Google I am getting the same result. I am not a confident user of VBA so any help would be appreciated from anyone.
 
Have you tried the code I posted?

Cheers
Paul Edstein
[MS MVP - Word]
 
Paul, Yes I tried it but I just got "Run-time error '5824' Method 'TextToDisplay' of object 'Hyperlink' failed
 
@kjv: If I'm not mistaken, "ThisDocument" refers to the document containing the code which in this case would be Normal.dot. Plus: I wouldn't want to click 700 messageboxes. [tongue]
@Paul: if Bill mixed your code with kjv's the wrong way, that might be the cause.

@Bill: try running this:
Code:
Sub ChangeAddresses
Dim hl As Hyperlink

For Each hl In ActiveDocument.Hyperlinks
    If InStr(1, hl.Address, "[URL unfurl="true"]http://cyscoshare/")[/URL] > 0 Then
        hl.Address = Replace(hl.Address, "[URL unfurl="true"]http://cyscoshare/",[/URL] "[URL unfurl="true"]http://cyscoshare05/")[/URL]
        hl.TextToDisplay = hl.Address
    End If
Next

End Sub

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
I see.
Just leave away this line from the code:
Code:
hl.TextToDisplay = hl.Address

So that it becomes this:
Code:
Sub ChangeAddresses
Dim hl As Hyperlink

For Each hl In ActiveDocument.Hyperlinks
    If InStr(1, hl.Address, "[URL unfurl="true"]http://cyscoshare/")[/URL] > 0 Then
        hl.Address = Replace(hl.Address, "[URL unfurl="true"]http://cyscoshare/",[/URL] "[URL unfurl="true"]http://cyscoshare05/")[/URL]
    End If
Next

End Sub

P.S: I hope you are making backups... [tongue]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
MakeItSo: Yes that works perfect. Thank you for your Patience.
 
ThisDocument does reference the doc in which the code resides. My assumption was that the code wouldn't be something the user would want to use on multiple docs, but rather the doc in question. So in that case, they would put the code inside the document that has all the links, run the code, then be done with it. But yes, if I were to use it myself, I'd likely prefer to have it available at any time for any doc, and would use ActiveDocument instead. [smile]

I did specify that in my original post:
me said:
The code you have to put in the particular Word Doc in order to use the "ThisDocument" object. You could use a more general document object if you want to put it in Normal.dot or Normal.dotx.

And InStr(1, hl.Address, " > 0

vs

InStr(1, hl.Address, "
will give the same results.

I suppose the "O" message came in where the user put the code under Normal.Dot(x) instead of the current file.

glad it worked out.

One other note of clarification: Bill4tektips didn't ask to replace the displayed text with the link, but it was given. When that example was given, Bill simply added it to the code. Then Bill said he didn't want it after all, which wound up with a different variation of the original code.

In the end, hopefully Bill4tektips learned something of future value. [thumbsup2]


"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top