Document Editing Problems

Use Word VBA to Check Hyperlinks and Add Comments

Bushra ParveenBushra Parveen Nov 22, 2026 868 views

Question details

The user wants to test hyperlinks in a Word document with VBA and add a comment to links that do not return a successful HTTP response.

Use Word VBA to Check Hyperlinks and Add Comments
Product
Microsoft Word VBA
Device & OS
not provided
Scenario
Review hyperlinks in the active document using a WinHTTP request.
Observed behavior
The desired result is a comment saying “Link doesn't work” on each failed link, with uncertain results reviewed separately.
Before you start

This example requires desktop Word on Windows with access to WinHTTP. Open an editable document, allow macros only from a trusted source, and check that your network can reach the linked sites.

Solution 1Recommended

Run a Word VBA macro to check HTTP links and comment on failures

Use this macro to test external HTTP and HTTPS hyperlinks in the active document and annotate links that return an unsuccessful response.

The macro follows redirects and treats 2xx responses as successful. Authentication restrictions, rate limits, and request errors are counted for manual review rather than automatically marked as broken.

Internal document links, email links, and other non-HTTP addresses are skipped. Server errors and other non-2xx responses can also be temporary, so verify comments before changing links.

1
Open the VBA editor

In desktop Word, open the document and press Alt+F11. In the VBA editor, select Insert > Module.

2
Insert the macro

Paste the following code into the module: Option Explicit Sub CheckDocumentHyperlinks() Dim link As Hyperlink Dim url As String Dim result As Long Dim checked As Long, failed As Long, review As Long, skipped As Long For Each link In ActiveDocument.Hyperlinks url = Trim$(link.Address) If LCase$(Left$(url, 7)) = "http://" Or _ LCase$(Left$(url, 8)) = "https://" Then checked = checked + 1 result = CheckHttpLink(url) If result = 0 Then ActiveDocument.Comments.Add Range:=link.Range, Text:="Link doesn't work" failed = failed + 1 ElseIf result = -1 Then review = review + 1 End If Else skipped = skipped + 1 End If Next link MsgBox "HTTP links checked: " & checked & vbCrLf & _ "Comments added: " & failed & vbCrLf & _ "Need manual review: " & review & vbCrLf & _ "Non-HTTP links skipped: " & skipped, vbInformation End Sub ' Returns 1 for success, 0 for a failed response, and -1 for review. Private Function CheckHttpLink(ByVal url As String) As Long Dim request As Object Dim status As Long On Error GoTo RequestError Set request = CreateObject("WinHttp.WinHttpRequest.5.1") request.SetTimeouts 5000, 5000, 10000, 10000 request.Open "GET", url, False request.Option(6) = True request.Send status = request.Status If status >= 200 And status < 300 Then CheckHttpLink = 1 ElseIf status = 401 Or status = 403 Or status = 429 Then CheckHttpLink = -1 Else CheckHttpLink = 0 End If Exit Function RequestError: CheckHttpLink = -1 End Function

3
Run and review the results

Return to Word, press Alt+F8, select CheckDocumentHyperlinks, and click Run. Review the comments in the document and manually test links that may require sign-in or special access.

Run a Word VBA macro to check HTTP links and comment on failures
Avoid duplicate comments: Running the macro again can add another comment to the same failed link. The summary counts uncertain requests but does not identify their individual addresses; check those links manually if needed.
Free Microsoft Office alternative

Review Word documents and hyperlinks in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface and Word-format support for a smooth move between editors. Use WPS Writer to open documents, review hyperlinks, and add comments; the Word-specific WinHTTP VBA macro is not a fix provided by WPS.

Review hyperlinks and add comments in WPS WriterOpen and edit Microsoft Word DOCX documentsWork in a familiar, easy-to-use interfaceUse a free, lightweight Office suite
QA img-9

Frequently Asked Questions

Why might a working link be marked as broken?

A site can return a temporary server error or respond differently to an automated WinHTTP request than to a browser. Open the link in a browser before editing it.

Does the macro check links to headings within the document?

No. It checks only addresses beginning with http:// or https://. Internal links generally use a SubAddress and are counted as skipped.

What happens when a site requires authentication?

Responses with status 401 or 403 are counted for manual review instead of receiving a broken-link comment. Open the link in a browser and sign in if needed.

Can I save the macro with the document?

In Word, use File > Save As and choose Word Macro-Enabled Document (*.docm) if you want to retain a macro stored in the document.