You want a specific VBA macro to execute the moment you open a Word document. This saves time by automating repetitive tasks such as applying formatting, updating headers, or refreshing data fields. Word provides two built-in event triggers, the Document_Open event and the AutoOpen macro, that run code automatically when a document loads. This article explains how to set up each method, where to place the code, and how to avoid common security blocks that prevent macros from running.
Key Takeaways: Automating Macro Execution on Document Open
- ThisDocument module in the VBA editor: Contains the Document_Open event that fires automatically when the document is opened.
- AutoOpen macro in a standard module or ThisDocument: A legacy method that also runs code when the document loads, but only in .doc or .docm files.
- File > Options > Trust Center > Trust Center Settings > Macro Settings: Enable all macros or digitally sign your macro to bypass the default security block.
How Word Triggers Macros on Document Open
Word supports two distinct mechanisms for running code automatically when a document is opened. The first is the Document_Open event, which is part of the ThisDocument class module. This event is the modern, recommended approach because it is explicitly tied to the document object and works reliably in all macro-enabled document formats (.docm, .dotm).
The second mechanism is the AutoOpen macro. This is a legacy feature from earlier versions of Word. When Word opens a document, it scans the project for a subroutine named AutoOpen and runs it. The AutoOpen macro can reside in a standard module or in the ThisDocument module. Both methods achieve the same result, but Document_Open gives you more control and is less likely to conflict with other add-ins.
Before you write any code, ensure your document is saved as a macro-enabled file. Use the .docm extension for documents and .dotm for templates. If you save as .docx, Word strips all VBA code and the macro will not run. Also, you must adjust Word macro security settings to allow macros to execute. By default, Word disables all macros without notification. You must either enable all macros, which is risky, or digitally sign your macro project with a self-signed or trusted certificate.
Steps to Add a Document_Open Event to Run a Macro Automatically
The Document_Open event is the most reliable method for triggering code when the document opens. Follow these steps to add the event handler and your custom macro.
- Open the VBA editor
Press Alt+F11 in Word. The Visual Basic for Applications editor opens. In the Project Explorer pane on the left, locate your document project. It will be listed as Normal (for the global template), Project (Document1), or the name of your saved document. - Double-click ThisDocument
In the Project Explorer, expand the Microsoft Word Objects folder under your document project. Double-click the ThisDocument entry. The code window for the ThisDocument module opens. - Select the Document event from the dropdown
At the top of the code window, click the left dropdown and select Document. The right dropdown automatically shows (Declarations). After selecting Document, the right dropdown now lists all available document events. Click the right dropdown and select Open. The VBA editor creates a stub procedure: Private Sub Document_Open() with an End Sub line. - Write your macro code inside the event
Between Private Sub Document_Open() and End Sub, type the code you want to run. For example, to display a message box, type: MsgBox “Document opened successfully.” You can also call another macro by typing Call MyMacroName. - Save the document as a macro-enabled file
Press Ctrl+S. In the Save As dialog, set the file type to Word Macro-Enabled Document (.docm). Choose a location and click Save. If you see a warning about macros being disabled, click OK and proceed to adjust security settings. - Enable macros in Trust Center
Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros. To avoid enabling all macros permanently, select Disable all macros except digitally signed macros and sign your project. Click OK twice to close the dialogs. - Test the macro
Close the document. Reopen it. The macro should run automatically. If nothing happens, recheck the security settings and confirm the file extension is .docm.
Steps to Create an AutoOpen Macro as an Alternative
The AutoOpen method is simpler but less flexible than the Document_Open event. Use this approach if you are working with legacy documents or prefer the older syntax.
- Open the VBA editor
Press Alt+F11 to open the VBA editor. - Insert a new standard module
In the Project Explorer, right-click your document project and choose Insert > Module. A new module named Module1 appears in the Modules folder. - Write the AutoOpen subroutine
In the code window for Module1, type:Sub AutoOpen() MsgBox "The document has opened." End SubYou can replace the MsgBox line with any code you need.
- Save the document as .docm
Press Ctrl+S and select Word Macro-Enabled Document (.docm) as the file type. - Enable macros and test
Follow step 6 and 7 from the previous section to enable macros and test the document.
Common Issues When Macros Do Not Run on Open
Macro security blocks the code from running
Word blocks macros by default. Even if you wrote the code correctly, the macro will not execute unless you enable macros. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros for testing. For production, sign the macro with a digital certificate and use Disable all macros except digitally signed macros.
The document is saved as .docx instead of .docm
If you accidentally save the file as a standard Word Document (.docx), Word removes all VBA code without warning. Verify the file extension in File Explorer. If it ends in .docx, rename it to .docm or open the file, press Alt+F11, and re-add the code, then save as .docm.
The AutoOpen macro is inside the wrong module
AutoOpen must be a Public Sub in a standard module or in the ThisDocument module. If you place it inside a class module or a user form, Word will not find it. Move the subroutine to a standard module or ThisDocument.
The Document_Open event is misspelled or has incorrect parameters
The event name must be exactly Document_Open. Any variation, such as DocumentOpen or Doc_Open, will not fire. Delete the incorrectly named procedure and use the dropdown method described earlier to insert the correct stub.
Document_Open vs AutoOpen: Key Differences
| Item | Document_Open Event | AutoOpen Macro |
|---|---|---|
| Location | ThisDocument class module only | Standard module or ThisDocument |
| Trigger | Fires when the document object loads | Fires when Word opens the file |
| Scope | Specific to the document project | Runs in any document that contains it |
| Compatibility | Works in .docm and .dotm | Works in .doc, .docm, and .dotm |
| Parameter support | No parameters allowed | No parameters allowed |
| Best practice | Recommended for new projects | Legacy, avoid for new work |
You can now schedule any VBA macro to run automatically when a Word document opens using either the Document_Open event or the AutoOpen macro. Start by writing your macro code in the ThisDocument module or a standard module. Always save the file as a macro-enabled .docm file and adjust Trust Center settings to allow macro execution. For advanced automation, consider adding error handling inside the Document_Open event to log failures or display user prompts without crashing Word.