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
  • Last Modified:

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)
2. Replace IF URL starts with "X:\share\old_name" THEN "X:\share\old_name\link1.doc" > "X:\share\new_name\link1.doc"
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
Asked:
ftenkley
  • 9
  • 6
1 Solution
 
GrahamSkanRetiredCommented:
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
    Dim hyp As Hyperlink
    
    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

Open in new window

0
 
GrahamSkanRetiredCommented:
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 hyp 'As Word.Hyperlink
    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)
            For Each hyp In doc.Hyperlinks
                With hyp
                    .Address = Replace(.Address, strOldPath, strNewPath)
                    .TextToDisplay = Replace(.TextToDisplay, strOldPath, strNewPath)
                End With
            Next 'hyp
            doc.Close -1 'wdSaveChanges
        End If
    Next 'fsFile
    wdApp.Quit

Open in new window

0
 
ftenkleyAuthor 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GrahamSkanRetiredCommented:
Not really. Can you post a sample document?
0
 
ftenkleyAuthor Commented:
Please see example in attached document.
hyperlink.docx
0
 
GrahamSkanRetiredCommented:
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
 
ftenkleyAuthor 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
 
ftenkleyAuthor Commented:
If I copy the hyperlink and paste it to notepad; I get the following:
\\network\root\home\shared\...\...\test_folder\test.doc
0
 
GrahamSkanRetiredCommented:
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
 
GrahamSkanRetiredCommented:
I am studying this KB which might be relevant:

http://support.microsoft.com/kb/967538
0
 
GrahamSkanRetiredCommented:
Duh!

We have been looking at relative hyperlinks.

See this article: http://support.microsoft.com/kb/903163
0
 
ftenkleyAuthor 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
 
GrahamSkanRetiredCommented:
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
 
GrahamSkanRetiredCommented:
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
    Dim hyp As Hyperlink
    
    
    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

Open in new window

0
 
ftenkleyAuthor 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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