Changing from Absolute to Relative Hyperlinks

by Allen Wyatt
(last updated June 26, 2018)

4

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:

www.Tips.net

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:

http://www.Tips.net/

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adding Caption Labels

When using the captioning capabilities of Word, you aren't limited to the three default caption labels provided in the ...

Discover More

Single-Use Drop-Down List

Want to create an easy drop-down list? You can do so by using the data validation features of Excel.

Discover More

Determining a Random Value

Random values are often needed when working with certain types of data. When you need to generate a random value in a ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Hyperlinks Not Found

When creating hyperlinks in a document, it is important to remember the difference between absolute and relative ...

Discover More

Controlling How Word Displays ScreenTips

ScreenTips can appear with or without shortcut keys displayed in them. Here's how to control whether they appear or not.

Discover More

Adding Hyperlinks

Adding a hyperlink to a text selection is easy to do in Word. All you need to do is make a couple of clicks and specify ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 + 9?

2018-01-25 18:52:22

Laurie

This is a problem I need to also resolve. I have a website where my documents are and so the documents have web addresses for hyperlinks. I also want to supply the complete documentation on a USB but that means I don't know the value of sNewBase as it will depend on the users computer as to drive name of the USB.
The reason for the USB copy is so that the user doesn't have to have Internet access


2015-01-03 06:12:33

Fulano

Very difficult to adapt this solution to any other situation. For example changing absolute links on a local HD to relative links so it can be moved to someone else's computer.

Anyway, to do that, it is much easier to press ALT-F9 to show link code and the do CTRL-H to search and replace.


2014-02-02 18:04:20

Craig Small

I think that a simpler method of finding "J" would be to use the "InStrRev" function, rather than looping backwards through the string. I know that the loop will always work, but it does tend to rather obscure what you're trying to do. Just a thought.

William, perhaps a reular expression would be appropriate in this instance, which could allow for "http", followed by 0 or 1 letter, followed by "://". I'm not a regexp expert, but a little research would tun up the right kind of expression, I'm sure. Maybe a little more complex than needed, given the purpose of the macro, but it would be very flexible ...


2014-02-02 15:22:21

William

Surely you would also need to anticipate secure internet addresses (ie, those that start with "https:"). These seem to be increasingly common nowadays.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.