Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Dynamically replacing relative path hyperlink with absolute path

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jenn - 18 Jan 2005 23:03 GMT
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.
Peter - 19 Jan 2005 01:42 GMT
> I'm using Word 2002 SP3 (10.5522.6714).  
> We are looking at the possibility of having to change the hyperlinks from
[quoted text clipped - 4 lines]
> 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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.