How to Use a Word VBA Macro to Hide Field Codes When Opening a Document
Question details
The user wants to ensure that documents automatically display field results instead of raw field codes upon opening, either by utilizing a VBA macro or a Word add-in.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Opening Word documents that contain dynamic fields like dates, page numbers, or mail merges.
- Observed behavior
- The user needs the application to default to hiding the field codes and showing the actual evaluated field results automatically when a file is opened.
Ensure your document macro security settings allow macros to execute, as Protected View or high security restrictions will prevent the AutoOpen macro from running.
Add an AutoOpen Macro to the Normal Template
Using an AutoOpen macro in your Normal template is the most direct way to force Word to hide field codes every time any document is opened.
The AutoOpen macro triggers immediately after a document is opened. By saving this macro in your global Normal template, the rule will apply to all documents you open on your computer.
Launch Microsoft Word and press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, locate the 'Normal' project. Right-click it, select 'Insert', and then click 'Module'.
In the new module window, type the following code: Sub AutoOpen() ActiveWindow.View.ShowFieldCodes = False End Sub
Save the changes to the Normal template and close the VBA editor. Open any Word document containing field codes to verify that they are now hidden by default.

Use a Dedicated Word Add-in
If you prefer not to modify your Normal template, you can use a custom add-in to handle document events and hide field codes automatically.
Automate Document Workflows with WPS Office
WPS Writer offers excellent VBA macro support, allowing you to run AutoOpen macros and manage field codes just as you would in Microsoft Word, all within a highly compatible and lightweight environment.
- 1. Access the Developer Tools: Open WPS Writer, navigate to the Developer tab on the ribbon, and click 'Macros' or press Alt+F11.
- 2. Input the Macro Code: Create a new module and input the standard AutoOpen macro code to set ShowFieldCodes to False.
- 3. Enjoy Automated Settings: Save your configuration. WPS Writer will consistently execute the macro, keeping your document views clean and free of exposed field codes.

Frequently Asked Questions
Why is my AutoOpen macro not working when I start the application?
The AutoOpen macro specifically runs when a document is opened, not when the Word application itself starts. If you need a macro to run immediately upon launching Word before opening any specific file, you must use an AutoExec macro instead.
Can I hide field codes manually without using a VBA macro?
Yes, you can manually toggle between viewing field codes and field results at any time by pressing Alt + F9 on your keyboard in Microsoft Word and WPS Writer.
Will document protection stop my macro from hiding field codes?
Yes, if a document is heavily protected or restricted from formatting and editing changes, it may interfere with the macro's ability to adjust the ActiveWindow view settings, preventing the field codes from hiding automatically.




