Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 475

# Replace hyperlinks (both text and url) in multiple Word files

I have several hundred Word files with many, many hyperlinks in them. These hyperlinks lead to other documents on our network share. Now, the folder names on the network share have changed, and therefor the links in the documents don't work anymore.

Is it possible to have a script, that will:
1. Open all Word documents (.doc and .docx) in a certain folder
2. Replace all the hyperlinks in the document that meet a certain criteria
3. Save and close the documents.

For example, the code would do the following;

1. Open test.doc (first document in the folder the code was started in)
3. <repeat for all links in this document>
4. Save and close test.doc
5. Open test2.doc (second document) > go to step 2

Any help is greatly appreciated!
0
ftenkley
• 9
• 6
1 Solution

RetiredCommented:
This Word macro will change both the address and the display:
Sub EditHypsInFolder()
Dim strFileName As String
Dim doc As Word.Document
Dim strFolder As String
Dim strOldPath As String
Dim strNewPath As String

strFolder = "C:\MyFolder"
strOldPath = "X:\share\old_name"
strNewPath = "X:\share\new_name"

strFileName = Dir$(strFolder & "\*.doc*") Do Until strFileName = "" Set doc = Documents.Open(strFolder & "\" & strFileName) For Each hyp In doc.Hyperlinks With hyp .Address = Replace(.Address, strOldPath, strNewPath) .TextToDisplay = Replace(.TextToDisplay, strOldPath, strNewPath) End With Next hyp doc.Close wdSaveChanges strFileName = Dir$()
Loop
End Sub

0

RetiredCommented:
Here is a VBScript version
Dim strFileName 'As String
Dim doc 'As Word.Document
Dim strFolder 'As String
Dim strOldPath 'As String
Dim strNewPath 'As String
Dim fso 'As FileSystemObject
Dim fsFolder 'As Folder
Dim fsFile 'As File
Dim wdApp 'As Word.Application

Set fso = CreateObject("Scripting.FileSystemObject")
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
strFolder = "C:\MyFolder"
strOldPath = "X:\share\old_name"
strNewPath = "X:\share\new_name"
Set fsFolder = fso.GetFolder(strFolder)
For Each fsFile In fsFolder.Files
If Left(fsFile.Type, 21) = "Microsoft Office Word" Then
Set doc = wdApp.Documents.Open(fsFile.PATH)
With hyp
.TextToDisplay = Replace(.TextToDisplay, strOldPath, strNewPath)
End With
Next 'hyp
doc.Close -1 'wdSaveChanges
End If
Next 'fsFile
wdApp.Quit

0

Author Commented:
Thanks Graham,

It seems the Word Macro is doing something (opening all documents in the strFolder), but the hyperlink (and text) isn't updated.

I guess this due to the fact that the hyperlink in Word doesn't look like X:\share\old_name\file.docx. Rather, it looks like file:///\\share\old_name\file.docx

I've tried changing strOldPath to file:///\\share\old_name - but that doesn't seem to do the trick. Any suggestions?

0

RetiredCommented:
Not really. Can you post a sample document?
0

Author Commented:
Please see example in attached document.
0

RetiredCommented:
Sorry for the delay.

There isn't a path in any of the Hyperlinks, so they just look for a file in the same folder.
0

Author Commented:
If I link to a document on our Shared folder (different server), it looks like what I written above:
file:///\\network\root\shared\...\...\folder-name01\test.doc

Does this mean the .Address doesn't work? I've changed the .TextToDisplay to = .Address, which does seem to change the text of the link. However, it seems the .Address line (19 in first code) doesn't work. Somehow it can't find the strOldPath, even if I change it to "file:///\\network\root" or "\\network\root\"

btw; thanks for your continuing help!

0

Author Commented:
If I copy the hyperlink and paste it to notepad; I get the following:
\\network\root\home\shared\...\...\test_folder\test.doc
0

RetiredCommented:
Sorry to have taken so long. I was reconnecting a laptop to act as a server, and there were multiple things wrong with the connection.

I have now tried it and it still works OK.

Here is my test document
HypDoc.docx
0

RetiredCommented:
I am studying this KB which might be relevant:

http://support.microsoft.com/kb/967538
0

RetiredCommented:
Duh!

We have been looking at relative hyperlinks.

0

Author Commented:
I've seen how it could be done differently in the future ((non-)relative links and such)... however, we're talking about a couple of hunderd documents here, with multiple thousands of links. Manually opening and adjusting the hyperlinks is not really something I would sign up for to do :)

So, does this means I can't use the macro to change all those hyperlinks?

PS: I've looked at your HypDoc.docx... you didn't include any code/macro's, did you? (i found none)

0

RetiredCommented:
No, it was to show you something like what I could see in yours. I'll see if I can come up with something to suit your situation.
0

RetiredCommented:
Hyperlinks don't seem to have an 'Absolute' or a 'Relative' property.  Relative ones just lack the path, so see if this helps.
Sub EditHyps()
Dim strFileName As String
Dim doc As Word.Document
Dim strFolder As String
Dim strOldPath As String
Dim strNewPath As String

strFolder = "I:\Allwork\ee\27413607"
strOldPath = "X:\share\old_name"
strNewPath = "X:\share\new_name"

strFileName = Dir$(strFolder & "\*.doc*") Do Until strFileName = "" Set doc = Documents.Open(strFolder & "\" & strFileName) For Each hyp In doc.Hyperlinks With hyp .Address = Replace(.Address, strOldPath, strNewPath) If InStr(.Address, "\") = 0 Then .Address = strNewPath & "\" & .Address End If .TextToDisplay = Replace(.TextToDisplay, strOldPath, strNewPath) End With Next hyp doc.Close wdSaveChanges strFileName = Dir$()
Loop
End Sub

0

Author Commented:
Hi Graham,

Almost :) ... I changed the following and it worked:
strOldPath = "\\root\shared\old_folder"
strNewPath = "\\root\shared\new_folder"

Thanks so much for helping me out.
0

## Featured Post

• 9
• 6
Tackle projects and never again get stuck behind a technical roadblock.