logo
search
Others

How to Use a Word VBA Macro to Hide Field Codes When Opening a Document

Kushani NimanthikaKushani Nimanthika Oct 12, 2026 871 views

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.

How to Use a Word VBA Macro to Hide Field Codes When Opening a Document
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.
Before you start

Ensure your document macro security settings allow macros to execute, as Protected View or high security restrictions will prevent the AutoOpen macro from running.

Solution 1Recommended

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.

1
Open the VBA Editor

Launch Microsoft Word and press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a Module into the Normal Template

In the Project Explorer pane on the left, locate the 'Normal' project. Right-click it, select 'Insert', and then click 'Module'.

3
Add the AutoOpen Code

In the new module window, type the following code: Sub AutoOpen() ActiveWindow.View.ShowFieldCodes = False End Sub

4
Save and Test

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.

Add an AutoOpen Macro to the Normal Template
Protected View Limitation: If you open a document downloaded from the internet or as an email attachment, Word may open it in Protected View. This security feature will block the AutoOpen macro from executing until you click 'Enable Editing'.

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. 1. Access the Developer Tools: Open WPS Writer, navigate to the Developer tab on the ribbon, and click 'Macros' or press Alt+F11.
  2. 2. Input the Macro Code: Create a new module and input the standard AutoOpen macro code to set ShowFieldCodes to False.
  3. 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.
Fully compatible with Microsoft Word documents (.docx) and VBA macros.Free, lightweight, and fast-loading alternative for daily office tasks.Familiar user interface makes transitioning and finding developer tools seamless.
microsoft office alternative - wps office

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.