Dynamically replacing relative path hyperlink with absolute path

J

Jenn

I'm using Word 2002 SP3 (10.5522.6714).
We are looking at the possibility of having to change the hyperlinks from
relative to absolute paths in thousands of documents stored in multiple
folders stored on a single drive.
Does anyone know of a way to replace these dynamically with a program?
Currently, the hyperlinks are mostly just a document name since the other
document is in the same folder. I need to be able to grab the complete path
and add it to the file name in the hyperlink.
 
P

Peter

Jenn said:
I'm using Word 2002 SP3 (10.5522.6714).
We are looking at the possibility of having to change the hyperlinks from
relative to absolute paths in thousands of documents stored in multiple
folders stored on a single drive.
Does anyone know of a way to replace these dynamically with a program?
Currently, the hyperlinks are mostly just a document name since the other
document is in the same folder. I need to be able to grab the complete path
and add it to the file name in the hyperlink.

I've come up with a few subs that will iterate through a folder tree, open each Word document, iterate through each document's hyperlinks, and attempt to make that hyperlink absolute. What I've found, though, is that it doesn't stick. The hyperlink's Address appears to become absolute, but when you open the document again, it is no longer absolute, and when you move that document, all the links are broken, as if you'd never messed with them.
Perhaps someone can share the deep dark secrets of file hyperlinks in Word with us, or perhaps I'll google it to see what I can find.

hth,

-Peter

Either way, here's what I came up with:

Option Explicit

Sub FindAndFixHyperlinks()
Dim oFS As New Scripting.FileSystemObject
Dim RootFolder As String

Call FixHyperLinks("C:\Documents and Settings\peter\Desktop\Testing Hyperlinks here.doc")

RootFolder = "U:\peter\docs"

If oFS.FolderExists(RootFolder) Then
Call findDocs(oFS.GetFolder("U:\peter\docs"))
End If

Set oFS = Nothing
End Sub

Private Sub findDocs(root As Folder)
Dim oFolder As Folder
Dim oFile As File

For Each oFolder In root.SubFolders
Call findDocs(oFolder)
Next oFolder

For Each oFile In root.Files
If oFile.Type = "Microsoft Word Document" Then
Call FixHyperLinks(oFile.Path)
End If
Next oFile

Set oFile = Nothing
Set oFolder = Nothing
End Sub

Private Sub FixHyperLinks(FileName As String)
Dim oFS As New Scripting.FileSystemObject
Dim oDoc As Document
Dim oHyp As Hyperlink

Set oDoc = Application.Documents.Open(FileName, , , , , , , , , , , False)

For Each oHyp In oDoc.Range.Hyperlinks
If oFS.FileExists(oHyp.Address) Then
oHyp.Address = oFS.GetFile(oHyp.Address).Path
End If
Next oHyp

Call oDoc.Close(True)

Set oHyp = Nothing
Set oDoc = Nothing
Set oFS = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top