Is there some method by which a list of all hyperlinks in a Microsoft Office 2010 Document can be obtained?
I'm trying to check a large number of large documents (a grab bag of Word, Excel and PowerPoint documents) for broken links, and I'd rather not have to read every line of every document to verify that I have a list of all links.
3 Answers
For MS WORD,
Press Alt + F9 to display the fields
Ctrl + F to open the search box
Search: ^d hyperlink
Check "Highlight all items found ..."
Click on the Find All button
Close the dialog
Ctrl + C to copy everything that is highlighted
Open a new document and paste.For Excel,
Close all workbooks except the one you want to find the links in.
On the Edit menu, click Find.
Click Options.
In the Find what box, enter [.
In the Within box, click Workbook.
In the Look In box, click Formulas.
Click Find All.
In the box at the bottom, look in the Formula column for formulas that contain [.
To select the cell with a link, select the row in the box at the bottom.
Links are also commonly used in names, text boxes, or chart titles. 7 To list all hyperlinks in a Word document:
Sub CheckLinks() Set doc = ActiveDocument Dim i For i = 1 To doc.Hyperlinks.Count Debug.Print doc.Hyperlinks(i).Address & " " & doc.Hyperlinks(i).SubAddress Next
End Sub 1 I really found the answer of @user228546 helpful, as I could not get my version of Microsoft Word (2013) to show me the options in the accepted answer. However, it's a little brief, and it requires a good knowledge of Visual Basic for Applications (VBA) to get everything to work.
Here's a slightly modified answer that could help some people who don't know so much about VBA.
You'll need to get to the VBA editor using Alt+F11. Use "Insert" -> "Module" up at the top, which will get you an editor window.
Get the Link Addresses in a New Document
I'm actually going to save the extracted hyperlinks into a new document, which I'll then save.
Type (or copy/paste) the following into the editor window.
Sub GetLinksInNewDoc()
'
' Finds all hyperlinks (even with strange formats,
' as long as they're active)
' and displays them in a new document.
' ' Declare the types of our variables Dim doc As Document Dim newDoc As Document Dim hlink As Hyperlink ' Use the script on the current document Set doc = ActiveDocument ' Open a new document to put the link addresses into Set newDoc = Documents.Add ' Loop through all the hyperlinks using the iterable hlink variable With doc For Each hlink In .Hyperlinks ' Switch into the new document newDoc.Activate ' Put the Hyperlink Address in the new document With Selection .InsertAfter hlink.Address & " " & hlink.SubAddress .InsertAfter vbNewLine End With Next hlink End With Set doc = Nothing Set newDoc = Nothing
End SubMake sure that your Document with the hyperlinks is the last Microsoft Word document you had highlighted. Save your code. Either click on the green arrow to run, or from the upper toolbar select "Run" -> "Run Sub/UserForm", or press F5
Note that you might get a "grey ghost" of the text that will eventually be in the document - something like
Get the Link Addresses in a TXT File
Now, if you actually wanted to save the URLs to a TXT file, which is what got me to this question, you can use the same procedure, except your code should be
Sub GetLinksInTxtFile()
'
' Finds all hyperlinks (even with strange formats,
' as long as they're active)
' and outputs them to a TXT file.
' The TXT file will be written in the same directory
' as the original document
' ' Declare the types of our variables Dim doc As Document Dim hlink As Hyperlink ' Use the script on the current document Set doc = ActiveDocument ' Get a text file ready to which you will write the URLs ' Some old-school BASIC Open doc.Path & "\the_urls.txt" For Output As #1 ' Loop through all the hyperlinks using the iterable hlink variable With doc For Each hlink In .Hyperlinks Print #1, hlink.Address & " " & hlink.SubAddress Next hlink End With Close #1 Set doc = Nothing
End SubI hope it helps.
Source for my understanding, outline of copying into new document.
Source for writing to a text file (which is what I originally came searching for)