INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...Your site has saved me hours of work that I cannot begin to express my satisfaction..."
Geography
Where in the world do Tek-Tips members come from?
|
Change Hyperlinks in Word
|
|
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 http://cyscoshare/ to http://cyscoshare05/. Can anyone help? |
|
|
kjv1611 (TechnicalUser) |
29 Jun 12 11:17 |
Well, just go through, change each one, link by link, and take a coffee break between each change. Will that work?
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):
CODESub 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...
CODESub 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 http://www.google.com
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 |
|
|
macropod (TechnicalUser) |
29 Jun 12 19:40 |
After doing the Find/Replace, you could run the following macro:
CODE --> VBASub 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. 
@Paul: if Bill mixed your code with kjv's the wrong way, that might be the cause.
@Bill: try running this:
CODESub ChangeAddresses
Dim hl As Hyperlink
For Each hl In ActiveDocument.Hyperlinks
If InStr(1, hl.Address, "http://cyscoshare/") > 0 Then
hl.Address = Replace(hl.Address, "http://cyscoshare/", "http://cyscoshare05/")
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:
CODEhl.TextToDisplay = hl.Address
So that it becomes this:
CODESub ChangeAddresses
Dim hl As Hyperlink
For Each hl In ActiveDocument.Hyperlinks
If InStr(1, hl.Address, "http://cyscoshare/") > 0 Then
hl.Address = Replace(hl.Address, "http://cyscoshare/", "http://cyscoshare05/")
End If
Next
End Sub
P.S: I hope you are making backups... “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. |
|
|
kjv1611 (TechnicalUser) |
2 Jul 12 12:58 |
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.
I did specify that in my original post:
Quote (me)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, " http://cyscoshare/") > 0
vs
InStr(1, hl.Address, " http://cyscoshare/")
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.
"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57 |
|
|
 |
|