logo
search
VBA & Macro Problems

How to Convert HTML Anchor Tags to Hyperlinks in Word with VBA

Steve KSteve K Oct 10, 2026 869 views

Question details

The user wants to process a Word document containing raw HTML anchor tags, removing the HTML code and replacing it with clickable hyperlinks that preserve the designated title text.

Convert HTML Anchor Tags to Hyperlinks in Word using VBA
Product
Word
Device & OS
not provided
Scenario
Formatting a document that contains raw, unrendered HTML anchor tags exported or copied from another source.
Observed behavior
The document currently displays literal HTML code (e.g., <a href="https://website.com">Title</a>) instead of functional, clickable hyperlinks.
Before you start

Verify that all HTML anchor tags in your document have correctly formatted opening (<) and closing (>) angle brackets, as missing brackets will cause the VBA macro's search pattern to fail.

Solution 1Recommended

Use a VBA Macro to Convert Anchor Tags

Create and run a custom VBA script to automatically parse the document for anchor tags, extract URLs and text, and replace them with active Word hyperlinks.

This method uses Word's Find feature with wildcards to locate HTML anchor structures. The macro extracts the URL from the href attribute and the display text, then replaces the raw code using the ActiveDocument.Hyperlinks.Add method.

1
Open the VBA Editor

In your Word document, press the 'Alt + F11' keys simultaneously to open the Microsoft Visual Basic for Applications (VBA) window.

2
Insert a New Module

In the VBA editor, click on 'Insert' in the top menu bar and select 'Module' to create a blank script window.

3
Write the Macro Code

Enter a VBA script designed to use wildcard search (e.g., \<a href="*"\>*\</a\>) to find the tags. The script must parse the found text to separate the URL and the title, then apply the ActiveDocument.Hyperlinks.Add function.

4
Run the Macro

Press 'F5' on your keyboard or click the 'Run' icon in the toolbar. The macro will scan the entire document, systematically replacing all correctly formatted anchor tags with clickable hyperlinks.

Use a VBA Macro to Convert Anchor Tags
Check Tag Formatting: If the macro skips certain links or throws an error, carefully check those specific lines in your document. Accidental deletion of a closing angle bracket is a common reason for the macro failing to recognize the tag.
Advanced Document Processing

Easily Manage Macros and Hyperlinks in WPS Writer

WPS Office offers robust document editing capabilities and macro support, allowing you to automate repetitive formatting tasks like converting HTML tags into clickable links quickly and reliably.

  1. 1. Open Your Document: Launch WPS Writer and open the document containing your unformatted HTML anchor tags.
  2. 2. Access Macro Tools: Navigate to the 'Developer' tab on the ribbon and click on 'Macros' or 'Visual Basic' to open the code editor.
  3. 3. Run Formatting Scripts: Paste your hyperlink conversion script into the module and execute it to instantly clean up your document formatting.
Fully compatible with Microsoft Word DOCX formats and existing VBA scriptsLightweight and fast, ensuring complex macros run smoothly across large documentsClean, intuitive interface for managing developer tools and document formattingFree to download and use for your daily office tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro stop or fail on certain HTML tags?

This usually happens if the HTML tags are malformed. Check your document to ensure that every opening tag (<) has a corresponding closing angle bracket (>). Missing brackets or mismatched quotation marks will break the VBA search pattern.

Does this VBA method work for different types of URLs?

Yes, as long as the macro is designed to capture the entire string inside the href attribute, it will successfully parse and create hyperlinks for standard web addresses (http/https), FTP sites, and other valid URL formats.

Can I use this method to convert other HTML elements in Word?

This specific macro logic targets anchor tags specifically. To convert other HTML elements (like bold tags or lists), you would need to write separate VBA Find and Replace parameters, or alternatively use the method of saving the text as an HTML file and letting Word render all elements natively.