Counting Internal Links

Written by Allen Wyatt (last updated May 29, 2023)
This tip applies to Word 2007, 2010, and 2013


3

Word allows you to establish links in a document to either an external document or to other places within the current document. Peter is looking for a way to count only the internal links within a document and wonders if there is a way to do this.

The only way you can do this is by using a macro to step through each hyperlink in your document. Fortunately, this is easily done by examining the elements in the Hyperlinks collection. Each Hyperlink object has a variety of properties that can be set, depending on the characteristics of the link. Of particular importance are the Address and SubAddress properties.

The Address property (as its name suggests) is the target address for the link. Normally this is something like a URL (as in http://www.tips.net), but it could also be the name of a file on a drive accessible from your system or an e-mail address (as in mailto:allen@sharonparq.com). Because the Address property can have multiple purposes, you cannot specifically check for the existence of telling characters, such as the "http" prefix. If the prefix is missing, then the link can still be external to the current document because it may reference a different file on your system.

There is one instances, however, when the Address field will be blank—if the link is to a bookmark within the current document. If it is blank, then the SubAddress property will be set to the name of the bookmark being referenced in the link. Of course, if the link is to a specific bookmark in a different document then both the Address and SubAddress properties will be set.

To get an idea of the information stored with each Hyperlink object, take a look at the following short macro. It steps through each link and displays information about each.

Sub LinkInfo()
    Dim h As Hyperlink
    Dim sTemp As String

    For Each h In ActiveDocument.Hyperlinks
        sTemp = h.TextToDisplay & vbCrLf
        sTemp = sTemp & h.Address & vbCrLf
        sTemp = sTemp & h.SubAddress
        MsgBox sTemp
    Next h
End Sub

So, the easiest method to determine the number of internal links within a document (in other words, links to bookmarks within the current document) is to examine the Address property of each Hyperlink object. If the property is empty, then you can safely assume that the link is internal.

Sub InternalLinks1()
    Dim h As Hyperlink
    Dim lInternal As Long

    lInternal = 0
    If ActiveDocument.Hyperlinks.Count > 0 Then
        For Each h In ActiveDocument.Hyperlinks
            If h.Address = "" Then lInternal = lInternal + 1
        Next hp
    End If
    MsgBox lInternal & " hyperlinks are internal" _
      & " out of a total of " & ActiveDocument.Hyperlinks.Count
End Sub

If you wanted to put together a list of the actual targets for the internal links, it would be an easy addition to the macro to look, within the For Each loop, at each SubAddress property and add it to whatever you displayed at the end of the macro.

Of course, hyperlinks aren't limited to appearing solely within the body of the document. You could also have hyperlinks in other document elements, such as headers, footers, endnotes, and text boxes. The following variation of the macro counts all the links it finds in any of these area (StoryRanges) in the document.

Sub InternalLinks2()
    Dim h As Hyperlink
    Dim lInternal As Long
    Dim lTotal As Long
    Dim aStory As Range

    lTotal = 0
    lInternal = 0
    For Each aStory In ActiveDocument.StoryRanges
        lTotal = lTotal + aStory.Hyperlinks.Count
        If aStory.Hyperlinks.Count > 0 Then
            For Each h In aStory.Hyperlinks
                If h.Address = "" Then lInternal = lInternal + 1
            Next h
        End If
    Next aStory
    MsgBox lInternal & " hyperlinks are internal" _
      & " out of a total of " & lTotal
End Sub

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 (12814) 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

Formula Shows Instead of Formula Result

When you enter a formula in a cell, you expect Excel to display the result of that formula once you are completed. If ...

Discover More

Automatically Formatting an ASCII File

Import a file from a source external to Word, and you can end up with a formatting nightmare. One big step is to remove ...

Discover More

Printing Document Properties

Word maintains quite a bit of information about a document in a special collection of items called "properties." You can ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Associating a Name with a Position

Wouldn't it be great if Word allowed you to have a small pop-up that showed you some information associated with a ...

Discover More

Pop-up Windows in Word

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 ...

Discover More

Word Link to Create a New Excel Workbook

It's easy to create and include links in your documents to other sources, in and out of Word. There are some limitations ...

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}] (all 7 characters, in the sequence shown) 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 2 + 2?

2016-04-24 08:57:40

K.A. Zweig

Dear Allen,
thanks a lot for providing the code for this interesting question. Do you have any idea on how to enhance it such that I can test whether the targets of an internal links are still in the document?
I am writing a series of documents with a glossary. Most terms will be in all documents but some are not necessary for all of them. THus, I would like to reduce this glossary to the entries that each document really needs. While I am deleting entries, I would like to check whether all links are still meangingful.
Would you know how to test that?
Thanks in advance for sharing your knowledge!
Katharina


2013-12-01 16:03:43

William

It might be wrong to presume that the existence of an Address means that a hyperlink is external. It's possible to include the current file name when creating a link to a bookmark in the current file, and I've seen it done. It would be safer then, to count a link as internal if the Address is empty or if the Address doesn't match the name of the active document.

Also, in the "InternalLinks1()" macro, "Next hp" should be "Next h".


2013-11-30 15:40:37

Ken Endacott

The macro InternalLinks2 doesn't necessarily find all hyperlinks in the document. It only looks for hyperlinks in the first textbox and not subsequent textboxes.

To properly search the document it is necessary to look at every shape in each story and if the shape has a textframe (the HasText property) then check its contents for hyperlinks. The story StoryRanges(wdTextframeStory) has to be be ignored otherwise the first textbox will be checked twice.


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.

Videos
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.