How to Fix a Word VBA Find and Replace Macro That Hangs
Question details
The user needs to resolve an issue where a Word VBA macro freezes or hangs indefinitely during the Find.Execute method when replacing placeholders, exporting PDFs, or using UndoRecord.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document generation, replacing text placeholders, and exporting files to PDF using VBA scripts.
- Observed behavior
- The VBA macro hangs or freezes at the Find.Execute step, preventing the script from completing the find and replace operation or the subsequent PDF export.
Ensure you have saved a backup of your Word document and macro code before making structural changes to your VBA script.
Replace Find and Replace with Document Variables (DOCVARIABLE)
Using DOCVARIABLE fields is a more stable and efficient method for replacing placeholders in Word VBA, avoiding the hanging issues caused by Find.Execute and UndoRecord.
When a macro hangs during a Find and Replace operation, it is often due to conflicts with the UndoRecord object or complex document structures. Switching to Document Variables eliminates the need for search loops and custom undo records, making the code run significantly faster and without freezing.
In your Word document, place your cursor where the data should appear. Press Ctrl+F9 to insert field braces, and type DOCVARIABLE VariableName inside them (for example, { DOCVARIABLE XXdataXX }).
In your VBA editor, replace your Find and Replace code with a variable assignment: ThisDocument.Variables("XXdataXX").Value = "Your Data Here".
Add the following line to your macro to refresh the document and display the new values: ThisDocument.Range.Fields.Update.
Delete any custom Application.UndoRecord declarations that were previously used to revert the Find and Replace actions, as updating fields simplifies document restoration.

Experience Stable Document Processing with WPS Office
If you frequently encounter freezing macros or performance issues in Microsoft Word, consider switching to WPS Office. It offers a lightweight, highly compatible environment for your document processing needs, ensuring smooth performance without heavy resource usage.
- 1. Download and Install: Get WPS Office Free from the official website and install it on your device.
- 2. Open Your Document: Launch WPS Writer and open your existing Word document (.docx) directly.
- 3. Edit Seamlessly: Enjoy a familiar interface and high format compatibility without the performance lag.

Frequently Asked Questions
Why does Find.Execute hang in Word VBA?
Find.Execute can hang due to infinite loops if the search criteria are not properly updated, conflicts with custom UndoRecord objects, or when processing highly complex document layouts like nested tables.
What is a DOCVARIABLE in Word?
A DOCVARIABLE is a hidden field in a Word document that stores data. It can be easily updated using VBA, making it a highly efficient alternative to finding and replacing text placeholders.
How do I view hidden field codes in Word?
You can toggle the display of field codes by pressing Alt+F9 on your keyboard. This allows you to see the underlying { DOCVARIABLE } tags instead of their values.
Can I use Document Variables to export PDFs?
Yes. You can update the Document Variables with the specific data for each record, update the fields, and then use the ExportAsFixedFormat method to generate the PDF without modifying the core document text.




