logo
search
VBA & Macro Problems

How to Use Word VBA to Add Plain-Text URLs After Hyperlinks

Huda QurayshiHuda Qurayshi Oct 19, 2026 869 views

Question details

The user wants to use a VBA macro to extract the destination URL from all hyperlinks in a document and display them in parentheses right after the clickable link.

Use Word VBA to Add Plain-Text URLs After Hyperlinks
Product
Word
Device & OS
not provided
Scenario
Automating document formatting to visibly display link addresses while ensuring the appended URL appears as regular text without inheriting the blue, underlined hyperlink style.
Observed behavior
When standard text insertion is used via VBA, the appended URL inherits the hyperlink's default blue color and underlining instead of appearing as plain text outside the link range.
Before you start

Ensure the Developer tab is enabled in your ribbon and save a backup of your document, as macro actions cannot always be undone with standard commands.

Solution 1Recommended

Run a VBA Macro to Append and Format URLs

Use this specific VBA script to iterate through all document hyperlinks, insert the URL behind the link, and explicitly remove hyperlink formatting from the inserted text.

A simple 'InsertAfter' command will cause Word to automatically apply the preceding hyperlink formatting to the newly added URL text. To prevent this, the macro must specifically select the newly added string and reset its font color and underline attributes.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a blank window for your code.

3
Paste the Macro Code

Copy and paste the following VBA code into the module window: Sub AppendPlainTextURLs() Dim link As Hyperlink Dim insertRange As Range Dim url As String For Each link In ActiveDocument.Hyperlinks url = link.Address If Len(link.SubAddress) > 0 Then If Len(url) > 0 Then url = url & "#" url = url & link.SubAddress End If Set insertRange = link.Range.Duplicate insertRange.Collapse wdCollapseEnd insertRange.InsertAfter " (" & url & ")" insertRange.Collapse wdCollapseEnd insertRange.MoveStart wdCharacter, -(Len(url) + 3) insertRange.Font.ColorIndex = wdAuto insertRange.Font.Underline = wdUnderlineNone Next link End Sub

4
Execute the Macro

Press F5 or click the 'Run' button (the green triangle) in the toolbar. The macro will process all links in the document, placing plain-text URLs in parentheses immediately following the clickable links.

Run a VBA Macro to Append and Format URLs
Formatting Preserved: The commands 'Font.ColorIndex = wdAuto' and 'Font.Underline = wdUnderlineNone' ensure the newly added URL adopts your document's default text style, leaving the original hyperlink intact.

Automate Documents Using WPS Writer

WPS Office provides excellent word processing capabilities, including support for VBA macros in its advanced versions, allowing you to easily run scripts to modify hyperlink text formats.

  1. 1. Download and Install WPS Office: Get the latest version of WPS Office and install it on your device.
  2. 2. Open Your Document: Launch WPS Writer and open the document containing the hyperlinks you wish to modify.
  3. 3. Use the Developer Tools: Navigate to the Developer tab, open the VBA Editor, and run your macro seamlessly to instantly reformat your hyperlinks.
Seamless compatibility with Microsoft Word formats (.doc, .docx)VBA macro support to automate repetitive text formatting tasksLightweight, efficient, and features a familiar user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does appended text inherit hyperlink formatting by default?

Word is designed to extend the formatting of preceding characters to newly typed or inserted text. Without explicitly clearing the font color and underline styles via code, VBA's InsertAfter command simply assumes the new text is part of the hyperlink range.

How do I enable the Developer tab in Word to use VBA?

Right-click anywhere on the main ribbon and select 'Customize the Ribbon'. In the right-hand list of main tabs, check the box next to 'Developer' and click OK.

Can I run this macro on only a selected portion of text?

Yes. You can modify the loop in the VBA script from 'For Each link In ActiveDocument.Hyperlinks' to 'For Each link In Selection.Hyperlinks'. This will force the macro to only append URLs for hyperlinks within the text you have currently highlighted.