How to Batch Validate All Hyperlinks in Your Word Document via VBA

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.Batch Validate All Hyperlinks in Your Word Document

Install and Save a Word Macro

  1. First of all, open VBA editor with “Alt+ F11”.
  2. Then click “Normal” project.
  3. Next click “Insert” tab on toolbar menu.
  4. And choose “Module” on the menu.Click "Normal"->Click "Insert"->Click "Module"
  5. 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
  1. Next click “Save”.Paste Codes->Click "Save"
  2. 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.

  1. Firstly, click “Insert” at toolbar again. But this time, choose “UserForm”.
  2. Next press “F4” to open “Properties Window” at the down-left corner. The window size is adjustable.
  3. Now name the user form as “frmCheckURLs”.
  4. Set caption as “Check URLs”.
  5. Then set font properly.
  6. Next click “Toolbox” button at toolbar.Click "Insert"->Choose "UserForm"->Click "Properties Window"->Set "Name", "Caption", "Font"->Click "Toolbox"
  7. Now click “Label” on toolbox. Use mouse to drag a rectangle label on the user form.
  8. Click on the label to activate its property window. Set the label name as “lblInvalidURLs”, and label caption as “Invalid URLs:”.
  9. Next choose proper font and forecolor for the label caption text.Click "Label"->Set "Name", "Caption", "Font" and "ForeColor"
  10. Choose “Text Box” on toolbox and insert a text box on the user form. Adjust its size.
  11. Then set text box name as “txtShowResult”.
  12. Set font and forecolor as desired.
  13. Find “MultiLine”, and set it “True”.
  14. Find “Scrollbars” and choose a vertical bar.Set Text Box's Name, Font, ForeColor ->Set "MultiLine" as "True"->Choose "frmScrollBarsVertical"
  15. Next create two more labels and text boxes.
  16. Then name first label as “lblTotalLinks” and set caption as “Total links in this document”.
  17. And name second label as “lblNumberOfInvalidLinks” and set caption as “Number of invalid links”.
  18. Name first text box as “txtTotalLinks”, and the second as “txtNumberOfInvalidLinks”.Create Two More Labels and Text Boxes
  19. Then find command button control on toolbox. Create two command buttons, such as bellow:Create Command Buttons
  20. Name the first button as “cmdbtnClose”, and its caption text as “Close”.
  21. Name the second button as “btnCloseAndHighlightInvalidURLs”, and its caption text as “Close & Highlight Invalid URLs”.
  22. Next double click on “Close” button and “Close & Highlight Invalid URLs” button respectively. Enter codes as follows:Enter Codes for Command Buttons

Run the Macro

Click the button you assign to the macro. Here is the final effect: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

4 responses to “How to Batch Validate All Hyperlinks in Your Word Document via VBA”

  1. This site was… how do I say it? Relevant!! Finally I have found something which helped me. Appreciate it!

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

Leave a Reply

Your email address will not be published. Required fields are marked *