by Allen Wyatt
(last updated February 6, 2018)
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:firstname.lastname@example.org). 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
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.
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!
Word, as you type, normally formats hyperlinks automatically. If you don't like the way that hyperlinks look in a ...Discover More
Word allows you to embed active hyperlinks in your documents. If you later want to change or edit that hyperlink, you can ...Discover More
Most people add hyperlinks in a document to reference pages on the Web. You can, however, create hyperlinks to other Word ...Discover More
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.