VBA & Macro Problems

How to Automatically Replace HTML Entities and Unicode Escapes in Word

Tauseeq MagsiTauseeq Magsi Nov 2, 2026 868 views

Question details

The user needs a method to automatically find and convert raw HTML entities and Unicode escape sequences into their corresponding actual characters within a Word document.

How to Automatically Replace HTML Entities and Unicode Escapes in Word
Product
Microsoft Word
Device & OS
not provided
Scenario
Cleaning up text files or exported documents that display raw code like '’' or '\u2019' instead of properly formatted characters.
Observed behavior
The document displays unconverted HTML entities and four-digit Unicode escape sequences which require bulk replacement, as manual editing would be too time-consuming.
Before you start

Before running any VBA scripts, ensure that you have enabled the Developer tab in your ribbon and make a backup copy of your original document to prevent accidental data loss.

Solution 1Recommended

Use a VBA Macro for Find and Replace

Execute a custom VBA script that combines standard text replacement for HTML entities with wildcard matching to dynamically calculate and replace Unicode escape sequences.

This macro searches the active document for specific HTML strings (like '’') and replaces them with standard punctuation. It then utilizes a wildcard pattern to locate sequences formatted as '\u' followed by four alphanumeric characters, converting the extracted hexadecimal values into real characters using the ChrW function.

1
Open the VBA Editor

Open your Word document and press 'ALT + F11' on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor, click on 'Insert' in the top menu bar, and then select 'Module' to create a blank workspace for your code.

3
Paste the Replacement Macro

Copy the provided VBA code (starting with 'Sub Replace_HTML_Codes()') and paste it into the new module window. Ensure the wildcard pattern '\\u[0-9e]{4}' matches the formatting in your document.

4
Run the Macro

Click anywhere inside the pasted code and press 'F5', or click the green 'Run' button in the toolbar. The macro will process the document and replace the designated codes automatically.

Use a VBA Macro for Find and Replace
Customizing the Macro: You can add additional 'With rg.Find' blocks in the VBA code to define more replacement rules for other specific HTML entities like '&' or '"' before executing the script.

Clean Up Document Text Using WPS Writer

WPS Office Writer provides powerful text formatting capabilities, including an Advanced Find and Replace tool with full wildcard support, and macro compatibility in supported versions. You can seamlessly clean up complex text documents.

  1. 1. Open Your Document in WPS Writer: Launch WPS Office and open the document containing the HTML entities or Unicode escapes.
  2. 2. Use Advanced Find and Replace: Press 'Ctrl + H' to open the Find and Replace dialog. Click 'Special' or 'More' to check the 'Use wildcards' option for pattern matching.
  3. 3. Execute Bulk Replacements: Enter your target HTML entity or wildcard sequence in the 'Find what' field, input the correct character in the 'Replace with' field, and click 'Replace All'.
Fully compatible with Microsoft Word .docx, .doc, and .txt formatsNative support for advanced Find and Replace with wildcard matchingLightweight, fast, and completely free core office suiteTabbed interface for managing multiple text clean-up tasks easily
microsoft office alternative - wps office

Frequently Asked Questions

Can I replace HTML entities without using a VBA macro?

Yes, for simple and known HTML entities (like & or "), you can use the standard Find and Replace tool (Ctrl+H). Type the entity code in the 'Find' box and the actual character in the 'Replace' box, then click 'Replace All'. However, macros are faster for documents with dozens of different entity types.

How do I use wildcards to find Unicode escapes manually?

Press Ctrl+H, click 'More', and check 'Use wildcards'. To find four-digit Unicode escapes starting with \u, type '\\u[0-9a-zA-Z]{4}' into the 'Find what' box. Note that you cannot dynamically calculate and replace these with their corresponding characters using standard manual Find and Replace, which is why a macro is recommended.

Why is the VBA macro missing some Unicode sequences?

The macro relies on strict pattern matching. If your document contains Unicode sequences with uppercase 'U' (\U) or variable lengths (like \u201 instead of \u0201), the specified wildcard pattern '\\u[0-9e]{4}' might not capture them. You may need to adjust the regular expression in the script to match your exact text formatting.

What does the ChrW function do in the provided VBA code?

The ChrW function in VBA converts a numeric character code (in this case, an extracted hexadecimal value from the text file) into its corresponding Unicode character. This allows the script to translate raw codes directly back into readable text.