by Allen Wyatt
(last updated February 1, 2014)
Linda has a Word document that contains many (1,800+) hyperlinks to resources on the Internet. She needs to convert all the hyperlinks so that they are not absolute to a URL on the Web, but so they point to a relative location on a USB drive. The name of the HTML file will be the same on the USB drive as it was on the Web, it is just the location of that HTML file that is changing. Linda wonders about the best way to do the conversion.
The best way is to do the conversions using a macro. Each hyperlink in your document is stored in the Hyperlinks collection which can be easily accessed via VBA. This allows you to step through the collection of hyperlinks, examine the addresses for each, and then make modifications in them. Here is an example of such a macro:
Sub ConvertHyperlinks() Dim sNewBase As String Dim sFile As String Dim sNewFile As String Dim sChanged As String Dim sNotChanged As String Dim sTemp As String Dim J As Integer Dim h As Hyperlink sNewBase = "c:\myplace\" sChanged = "" sNotChanged = "" For Each h In ActiveDocument.Hyperlinks sTemp = h.Address If Left(sTemp, 5) = "http:" Then sFile = "" For J = Len(sTemp) To 2 Step -1 If Mid(sTemp, J, 1) = "/" Then sFile = Right(sTemp, Len(sTemp) - J) Exit For End If Next J If sFile > "" Then sNewFile = sNewBase & sFile h.Address = sNewFile sChanged = sChanged & sTemp & " (changed to " & _ sNewFile & ")" & vbCrLf Else sNotChanged = sNotChanged & sTemp & vbCrLf End If Else sNotChanged = sNotChanged & sTemp & vbCrLf End If Next h Documents.Add Selection.TypeText "The following hyperlinks were modified:" & vbCrLf Selection.TypeText sChanged & vbCrLf & vbCrLf Selection.TypeText "The following hyperlinks were not modified:" & vbCrLf Selection.TypeText sNotChanged & vbCrLf End Sub
The macro starts by stepping through the Hyperlinks collection. Each address is assigned to the sTemp variable, which is then checked to see if it starts with "http:". Some hyperlinks—for example, those for e-mail addresses or to existing files—won't start with these characters. If a match is found, then the code steps backward through the address to find the final slash in that address. When it is found, then sFile is set to equal everything after that final slash, which means it will be equal to the HTML file.
An interesting sidenote here is that when you create a hyperlink in a word document, Word does a little bit of processing on what you type versus what is actually stored in the hyperlink. For instance, let's assume that you type the following in a document:
Word automatically recognizes this as a website and converts it to a hyperlink. (Assuming, of course, that you have Word set up to do automatic URL conversions.) If you look at the address actually stored in the created hyperlink, you'll see the following:
Because this address begins with "http:", the macro will consider it to be something processable. However, the loop that steps backward through the address looking for the last slash character will find it at the very end. This means that sFile will be empty because of this code:
sFile = Right(sTemp, Len(sTemp) - J)
The length of sTemp and J are the exact same for the last character in the string, so sFile ends up containing the rightmost 0 characters, meaning it is empty. The bottom line is that it won't contain a file name, so the original URL is not converted to something else.
To make the macro work properly on your system, you'll want to change the sNewBase string to contain whatever you want added to the beginning of the processed addresses. Note that as written, the default string added to the beginning of a file is "c:\myplace\". Thus "http://www.tips.net/privacy.html" is changed to "c:\myplace\privacy.html". This converted address is not, strictly, a relative hyperlink. A relative hyperlink would not have the c:\ characters at the beginning, so it would end up being something like "\myplace\privacy.html". Again, you can change the value of sNewBase to be anything you want so that your converted hyperlinks look just like you want them to.
It doesn't take long to run the macro; it is very fast, regardless of how many hyperlinks are in your document. When the macro is complete, it creates a new document that shows the hyperlinks modified and those not modified.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12931) applies to Microsoft Word 2007, 2010, and 2013.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Got a document that has a whole raft of e-mail address in it? You can easily convert all of them to clickable hyperlinks by ...Discover More
Want to add a small pop-up window over a word in your document? There is no way to do this directly in Word, but you can use ...Discover More
Wouldn't it be great if Word allowed you to have a small pop-up that showed you some information associated with a special ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.