Pulling All Hyperlinks

by Allen Wyatt
(last updated July 14, 2018)

10

Annemieke has a document that is rather long and it includes a good number of hyperlinks. She would like to copy all those hyperlinks (and just the hyperlinks) to a brand-new document so that she has a list of them in one place. She wonders if there is an easy way to do this.

The only way to do this is to use a macro. (Suggestions that include using Find and Replace or finding text with specific styles will copy only the link text, not the actual hyperlink.) The key in the macro is to work with the Hyperlinks collection, paying particular attention to the TextToDisplay and Address properties of each member of the collection.

Sub PullHyperlinks()
    Dim Src As Document
    Dim Link As Hyperlink
    Dim iDoDisplay As Integer

    Set Src = ActiveDocument
    If Src.Hyperlinks.Count > 0 Then
        iDoDisplay = MsgBox("Include display text for links?", vbYesNo)

        Documents.Add DocumentType:=wdNewBlankDocument
        For Each Link In Src.Hyperlinks
            If iDoDisplay = vbYes Then
                Selection.TypeText Link.TextToDisplay
                Selection.TypeText vbTab
            End If
            Selection.TypeText Link.Address
            Selection.TypeParagraph
        Next Link
    Else
        MsgBox "There are no hyperlinks in this document."
    End If
End Sub

The macro first checks to see if there are any hyperlinks in the current document. If there are, then the process of pulling them out can proceed; if there aren't, then a message is displayed to that effect.

Assuming there are hyperlinks in the document, the user is asked if the new document should contain the display text for the links. The macro then creates a new document and steps through each member of the Hyperlinks collection. The value of the TextToDisplay property is added to the new document (if appropriate) followed by the value of the Address property.

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 (13549) applies to Microsoft Word 2007, 2010, 2013, and 2016.

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

Converting Footnotes to Endnotes

When you spend a lot of time creating footnotes, how can you convert all of them to endnotes without entering them all ...

Discover More

Specifying Print Quantity in a Cell

When you print a worksheet, you can specify in the Print dialog box how many copies you want printed. If you want the ...

Discover More

Where Is that Text?

Looking for a formula that can return the address of a cell containing a text string? Look no further; the solution is in ...

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

Closing Documents after a Hyperlink

When you click a hyperlink that takes you to another document, Word dutifully opens the new document in its own window. ...

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
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 three minus 3?

2018-07-18 03:03:00

Stella

Hi Annemieke, I'm really glad to hear my solution worked for you. Regarding your question on how to have 2 columns, one with the screen text/regular text and one with the hyperlinks, my original solution would work with some extra steps.

1. Extract all the hyperlinks with my original solution and paste them in a new document.
2. Press Ctrl-A to select all the hyperlinks, and press Ctrl-Shift-F9 to "break" the hyperlinks. Now they will just be regular underlined black text.

To have a 2-column table, you could:
3. Convert the regular text into a table.
4. Add a new column.
5. Paste the extracted hyperlinks again OUTSIDE of the table. Convert the hyperlinks into a one-column table.
6. Copy the one-column table with the hyperlinks and paste it into the second column of your two-column table. The paste option that worked for me was 'Overwrite cells'.

I hope this solution is helpful!


2018-07-17 19:19:10

Ken Endacott

Annemieke,
Here is a complete macro solution that finds hyperlinks in all parts of the document including the bibliography. It produces a tab delimited list giving the document story that contains the hyperlink, the display text, the url and the intact hyperlink. This list can be converted to a table, select the list then: Insert > the down arrow on tables > Convert Text to Table.

Sub PullAllHyperlinks()
Dim Src As Document
Dim Link As Hyperlink
Dim iDoDisplay As Integer
Dim rng As Range
Dim k As Long
Dim objSource As Source
Dim strXML As String
Dim aShape As Shape

Set Src = ActiveDocument
ActiveWindow.View.ShowFieldCodes = False

iDoDisplay = MsgBox("Include display text for links?", vbYesNo)

Documents.Add DocumentType:=wdNewBlankDocument
For Each rng In Src.StoryRanges
Call WriteHyperlinks(rng, iDoDisplay, rng.StoryType)
Select Case rng.StoryType
Case 1, 2, 3, 4, 6, 7, 8, 9, 10, 11
If rng.ShapeRange.Count > 0 Then
For Each aShape In rng.ShapeRange
If aShape.TextFrame.HasText Then
If aShape.TextFrame.TextRange.Fields.Count > 0 Then
Call WriteHyperlinks(aShape.TextFrame.TextRange, iDoDisplay, rng.StoryType)
End If
End If
Next aShape
End If
End Select
Next rng

For Each objSource In Src.Bibliography.Sources
strXML = objSource.XML
k = InStr(1, strXML, "<b:URL>")
If k > 0 Then
strXML = Mid(strXML, k + 7)
k = InStr(1, strXML, "<")
strXML = Left(strXML, k - 1)
Selection.TypeText "Bibliography" & vbTab & strXML
Selection.TypeParagraph
End If
Next objSource

End Sub

Sub WriteHyperlinks(r As Range, L As Integer, n As Long)
Dim hLink As Hyperlink
Dim storyNme()
storyNme = Array("", "Body", "Footnotes", "Endnotes", "Comments", "Text frame", "Even pages header", _
"Primary header", "Even pages footer", "Primary footer", "First page header", _
"First page footer", "", "", "", _
"", "", "")

For Each hLink In r.Hyperlinks
Selection.TypeText storyNme(n) & vbTab
If L = vbYes Then
Selection.TypeText hLink.TextToDisplay
Selection.TypeText vbTab
End If
Selection.TypeText hLink.Address & vbTab
hLink.Range.Copy
Selection.Paste
Selection.TypeParagraph
Next hLink
End Sub


2018-07-17 11:21:42

Annemieke

I like Stella's approach because it a) kept the hyperlinks intact, and b) it provides me with an easy way to toggle between the text and the hyperlinks.
For my purpose: checking the hyperlinks - it works very well. It also shows me how many times I use a particular text or hyperlink, so I can use the list as a means to check x-references, logical relationships between paragraphs and generally improve the user experience.

Is there a way in which I can copy the field info - including the actual hyperlink - and paste that as regular text somewhere? That way I could create 2 columns: one with the screen text and one with the actual hyperlink (after some search and destroy).


2018-07-17 11:06:07

Annemieke

Great to have a solution so quickly! Thanks all for providing the main solution and the remarks.

I have tried out the macro and it creates a list alright, only the hyperlinks with me weren't hyperlinks once they were compiled into the list. They are plain text.

I tweeked the result a bit and will try to include that in the macro:
I first doubled the paragraph markers and then changed the tabs into paragraph markers, so I get:

Name / text to display
Hyperlink

Name / text to display
Hyperlink

etc.

I noticed that the internal hyperlinks (to other parts of the document) are empty. I resolved that by changing ^p^p^p into ^pInternal link^p^p.
That makes it a bit clearer.

Ideally the list could also show the paragraph or page number where the link appears in the list, so you'd really have a functioning register.

I'm now going to try Stella's solution :-)


2018-07-17 09:15:47

Ken Endacott

Here is a method that uses F&R to produce a comma delimited list of hyperlinks that can then be used to create a table of hyperlinks.

1. Work with a copy of the document. CTRL + A then set a font for the whole document – say Times New Roman
ALT + F9 to display field codes

Find: ^019 HYPERLINK

Replace: ^&,
Font: Arial

All of the document will now be in Times New Roman font except hyperlinks followed by a comma.

2. Use F&R to remove all characters in Times New Roman leaving a comma delimited list of hyperlinks.

Find: ?
Use Wildcards
Font: Times New Roman

Replace: leave blank and no formatting

Note that step 1 Use Wildcards is turned off but in Step 2 Use Wildcards must be on.


2018-07-17 05:19:10

Stella

Hi Ronald, thanks for your comment. Glad to know that my approach worked. I am not sure why ^19 HYPERLINK does not work for you. I did a quick Google search and found a Microsoft Community answer that suggests ^d is generic and finds any type of field - does ^d also pull out other non-hyperlink fields in your test document? I suspect ^019 HYPERLINK will find only 'hyperlink' fields.

https://answers.microsoft.com/en-us/mac/forum/macoffice2011-macword/findreplace-with-a-field-code/132acf8d-fa79-4fdb-a488-fa63c2459dcf?db=5


2018-07-16 14:58:33

Ronald Bolinger

In Stella's approach I was unable to find hyperlinks by using ^019 HYPERLINK in the search. I search ^d which locates all the hyperlinks in my test document. Otherwise, her approach works perfectly.


2018-07-16 14:25:42

Ronald Bolinger

That's not true. There is a method using Find and Replace which copies the actual hyperlink.


2018-07-16 04:20:34

Stella

I have found this approach useful:

1. Press Alt-F9 to display field codes.
2. Press Ctrl-H to display 'Find and Replace' and go to the 'Find' tab (not 'Find and replace').
3. Type '^019 HYPERLINK' in the 'Find' box.
4. Click 'Find in', and in the dropdown list select 'Main document'. This will select all the hyperlinks in the document.
5. Copy and paste the hyperlinks into a new document.
6. Press Alt-F9 again in the new document to un-display the field codes and display the normal blue hyperlink text.

You said in your article that 'approaches that use Find and Replace copy only the link text, not the actual hyperlink', but my approach does copy the actual hyperlink. It preserves the display text and the hyperlink just like your macro, and I think it's simpler too.


2018-07-14 05:29:43

Ken Endacott

The macro has severe limitations. It does not detect hyperlinks in headers, footers, textboxes, bibliography, footnotes and endnotes.

Documents for publication and academic documents typically have hyperlinks in footnotes, endnotes and the bibliography. It is important that all the links are checked for validity and relevance hence a complete list of hyperlinks is a necessity.


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.