In this article, we will show you an automatic way to quickly batch validate all hyperlinks in your Word document via VBA.
A Word document can contains many hyperlinks, hundreds even. Among them, some are invalid links that fail to lead you to a correct webpage. Thus, we want to offer you the way to quickly check all links in a document via VBA.
Install and Save a Word Macro
- First of all, open VBA editor with “Alt+ F11”.
- Then click “Normal” project.
- Next click “Insert” tab on toolbar menu.
- And choose “Module” on the menu.
- Double click on module to open editing space and paste the following codes there:
Function CheckURL(strURL As String) As Boolean Dim objDemand As Object Dim varResult As Variant On Error GoTo ErrorHandler Set objDemand = CreateObject("WinHttp.WinHttpRequest.5.1") With objDemand .Open "GET", strURL, False .Send varResult = .StatusText End With Set objDemand = Nothing If varResult = "OK" Then CheckURL = True Else CheckURL = False End If ErrorHandler: End Function Sub ReturnURLCheck() Dim objLink As Hyperlink Dim strLinkText As String Dim strLinkAddress As String Dim strResult As String Dim nInvalidLink As Integer, nTotalLinks As Integer Dim objDoc As Document Application.ScreenUpdating = False Set objDoc = ActiveDocument nTotalLinks = objDoc.Hyperlinks.Count nInvalidLink = 0 With objDoc For Each objLink In .Hyperlinks strLinkText = objLink.Range.Text strLinkAddress = objLink.Address If Not CheckURL(strLinkAddress) Then nInvalidLink = nInvalidLink + 1 strResult = frmCheckURLs.txtShowResult.Text frmCheckURLs.txtShowResult.Text = strResult & nInvalidLink & ". Invalid Link Information:" & vbNewLine & _ "Displayed Text: " & strLinkText & vbNewLine & _ "Address: " & strLinkAddress & vbNewLine & vbNewLine End If Next objLink frmCheckURLs.txtTotalLinks.Text = nTotalLinks frmCheckURLs.txtNumberOfInvalidLinks.Text = nInvalidLink frmCheckURLs.Show Modal End With Application.ScreenUpdating = True End Sub Sub HighlightInvalidLinks() Dim objLink As Hyperlink Dim strLinkAddress As String Dim strResult As String Dim objDoc As Document Set objDoc = ActiveDocument With objDoc For Each objLink In .Hyperlinks strLinkAddress = objLink.Address If Not CheckURL(strLinkAddress) Then objLink.Range.HighlightColorIndex = wdYellow End If Next objLink End With End Sub
- Next click “Save”.
- Then assign a button for the macro. You can refer to following article for detailed information:
How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word
Create a User Form
With a user form, you can clearly see the total number of links in current document, the number of invalid links and more details.
- Firstly, click “Insert” at toolbar again. But this time, choose “UserForm”.
- Next press “F4” to open “Properties Window” at the down-left corner. The window size is adjustable.
- Now name the user form as “frmCheckURLs”.
- Set caption as “Check URLs”.
- Then set font properly.
- Next click “Toolbox” button at toolbar.
- Now click “Label” on toolbox. Use mouse to drag a rectangle label on the user form.
- Click on the label to activate its property window. Set the label name as “lblInvalidURLs”, and label caption as “Invalid URLs:”.
- Next choose proper font and forecolor for the label caption text.
- Choose “Text Box” on toolbox and insert a text box on the user form. Adjust its size.
- Then set text box name as “txtShowResult”.
- Set font and forecolor as desired.
- Find “MultiLine”, and set it “True”.
- Find “Scrollbars” and choose a vertical bar.
- Next create two more labels and text boxes.
- Then name first label as “lblTotalLinks” and set caption as “Total links in this document”.
- And name second label as “lblNumberOfInvalidLinks” and set caption as “Number of invalid links”.
- Name first text box as “txtTotalLinks”, and the second as “txtNumberOfInvalidLinks”.
- Then find command button control on toolbox. Create two command buttons, such as bellow:
- Name the first button as “cmdbtnClose”, and its caption text as “Close”.
- Name the second button as “btnCloseAndHighlightInvalidURLs”, and its caption text as “Close & Highlight Invalid URLs”.
- Next double click on “Close” button and “Close & Highlight Invalid URLs” button respectively. Enter codes as follows:
Run the Macro
Click the button you assign to the macro. Here is the final effect:
You can see detailed information about each failed links on the left big text box. And you can choose to close the user form directly or close it while highlighting all broken links in document.
Handle Word Issues Carefully
Word is easily prone to errors. Thus it becomes extreme important to fix Word correctly. Most of the time, the built-in tool can’t help you so much. Therefore, you top choice is to get a third-party repairing tool.
Author Introduction:
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair xlsx and pdf repair software products. For more information visit www.datanumen.com
1
This site was… how do I say it? Relevant!! Finally I have found something which helped me. Appreciate it!
Thank you for sharing, it works great! I wanted to check if it is possible to create a Macro that would validate the links open in the right target or pull up the title of the target document and check if it matches the anchor text.
Thank you for sharing, this worked for me and simplified my work a lot!