How to Find and Replace Hyperlink Display Text in Word with VBA
Question details
The user needs to automate the process of finding specific reference URLs enclosed in curly braces, setting them as actual hyperlink addresses, and changing their visible display text to standardized labels like [DOI] or [PMID].

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Formatting academic or professional documents where numerous raw reference URLs need to be converted into clean, clickable hyperlink texts without default underlines.
- Observed behavior
- Raw URLs inside curly braces remain as static text and need to be transformed into active hyperlinks displaying a designated text placeholder.
Running a VBA macro will make permanent changes to the text and hyperlinks in your document, so always create and test on a backup copy first.
Use a VBA Macro to Process Hyperlinks in Word
This automated method uses a VBA script to search for URLs bounded by curly braces, turns them into active links, and updates the display text to a desired label like [DOI].
This approach uses Word's Find functionality within a VBA loop. It specifically targets text enclosed in curly braces. To apply this to PMID or PMCID references instead of DOIs, you will need to adjust the macro's replacement text accordingly.
Once the text is found, the macro adds a hyperlink to the selected range using the extracted URL as the address, changes the visible TextToDisplay property, and modifies the font to remove the underline.
Press Alt + F11 in Microsoft Word to open the Visual Basic for Applications (VBA) editor.
In the VBA editor, click on 'Insert' in the top menu and select 'Module' to create a new blank script window.
Paste your custom VBA code designed to search for curly braces using a wildcard search. Use the code to assign the found text to a string variable, and use 'ActiveDocument.Hyperlinks.Add' to set the 'TextToDisplay' to '[DOI]'.
Ensure your code includes a line addressing the font style, such as 'Selection.Font.Underline = wdUnderlineNone', to remove the default hyperlink underline formatting.
Close the VBA editor, return to your document, and press Alt + F8 to open the Macros dialog. Select your macro and click 'Run'.

Batch Replace Hyperlinks Easily in WPS Office
WPS Writer offers robust Macro capabilities, allowing you to seamlessly run VBA scripts to batch replace hyperlink text and update URL addresses just like you would in Microsoft Word.
- 1. Open your Document: Launch WPS Writer and open the document containing the reference links you need to format.
- 2. Access the Macro Editor: Navigate to the 'Tools' tab on the top ribbon and click on 'Macro' followed by 'Macros'.
- 3. Create a New Macro: Type a name for your new macro in the dialog box and click 'Create' to open the VBA editor.
- 4. Run the Script: Paste your find-and-replace hyperlink script into the editor and press F5 to execute it across your document.

Frequently Asked Questions
How can I adapt this macro for PMID and PMCID references?
You can duplicate the macro or add additional Find loops within the existing script. Simply change the search criteria if your PMIDs are enclosed in different characters, and update the 'TextToDisplay' property in the script from '[DOI]' to '[PMID]' or '[PMCID]'.
Why does my macro only replace the first hyperlink it finds?
Your VBA script likely lacks a loop. Ensure your Find and Replace code is wrapped inside a 'Do While .Execute' loop so that Word continues searching and formatting until it reaches the end of the document.
Can I remove the hyperlink underline without using a macro?
Yes. You can manually change the appearance of all hyperlinks in your document by opening the Styles pane (Alt + Ctrl + Shift + S), right-clicking the 'Hyperlink' style, selecting 'Modify', and toggling off the Underline formatting.




