You want a custom VBA macro to execute automatically every time you save a Word document. Word does not expose a direct setting to run a macro on save, but it provides a built-in event called DocumentBeforeSave that you can use. This article explains how to write and attach a macro to the DocumentBeforeSave event so that your code runs before the save operation completes. You will learn the exact steps to create the macro, test it, and handle common issues such as macro security and multiple document windows.
Key Takeaways: Automate a Macro on Document Save
- DocumentBeforeSave event in ThisDocument module: Triggers your macro code before any save action in that specific document.
- VBA project password protection: Prevents unauthorized users from viewing or altering the automatic macro.
- File > Options > Trust Center > Macro Settings > Enable all macros: Required to allow the DocumentBeforeSave event to run.
Understanding the DocumentBeforeSave Event in Word VBA
Word VBA provides event procedures that fire when specific actions occur. The DocumentBeforeSave event runs automatically whenever you save the document, whether you use Ctrl+S, click the Save icon, or close the document and choose to save changes. This event belongs to the Application object, but you can also handle it at the document level using the ThisDocument class module.
The event passes one argument: SaveAsUI, a Boolean that tells you whether Word is showing the Save As dialog. You can check this value inside your macro to decide whether to run certain code only during a full Save As operation.
Before you write the macro, ensure you have access to the Developer tab. If the Developer tab is not visible, go to File > Options > Customize Ribbon and check the Developer box in the right panel. You also need to allow macro execution in Trust Center settings. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros. For signed macros, you can choose Disable all macros except digitally signed macros instead.
Where to Place the Event Macro
You must place the DocumentBeforeSave macro inside the ThisDocument object of the specific document. Do not put it in a standard module. The ThisDocument module is visible in the Project Explorer under Microsoft Word Objects. Double-click ThisDocument to open its code window.
Steps to Create and Test a Macro That Runs on Document Save
Follow these steps to add a macro that displays a message box before every save. You can replace the message box code with your own logic, such as updating a timestamp, backing up the file, or validating content.
- Open the VBA Editor
Press Alt+F11 to open the Visual Basic for Applications editor. If the Project Explorer is not visible, press Ctrl+R to show it. - Locate the ThisDocument module
In the Project Explorer, expand the project that corresponds to your document. It usually appears as Project (DocumentName). Under Microsoft Word Objects, double-click ThisDocument. - Choose 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). Click the right dropdown and select DocumentBeforeSave. The VBA editor inserts the event procedure stub with End Sub. - Write your macro code inside the event procedure
Between Private Sub DocumentBeforeSave(ByVal SaveAsUI As Boolean, ByRef Cancel As Boolean) and End Sub, add your code. For example:MsgBox "Saving the document now. SaveAsUI = " & SaveAsUI - Test the macro
Close the VBA editor. Save the document by pressing Ctrl+S. A message box should appear before Word completes the save. Click OK to allow the save to finish. - Save the document as a macro-enabled file
Go to File > Save As. In the Save as type dropdown, select Word Macro-Enabled Document (docm). If you save as .docx, the macro and the event procedure are discarded.
Using the Cancel Argument to Stop a Save
The DocumentBeforeSave event includes a Cancel parameter. If you set Cancel = True inside the procedure, Word cancels the save operation. This is useful for validation: if certain conditions are not met, you can prevent the document from being saved. For example:
Private Sub DocumentBeforeSave(ByVal SaveAsUI As Boolean, ByRef Cancel As Boolean)
If Len(ActiveDocument.BuiltInDocumentProperties("Title")) = 0 Then
MsgBox "You must set a document title before saving."
Cancel = True
End If
End Sub
When Cancel is True, Word does not save the document. The user must correct the issue and save again.
Common Issues When Running a Macro on Document Save
The Macro Does Not Run When I Save
The most common cause is macro security settings. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros. Also verify that the document is saved as a .docm file. If the file extension is .docx, the VBA project is stripped and the event never fires.
Another cause is placing the code in a standard module instead of the ThisDocument module. Only the ThisDocument module can host document-level event procedures. Move the code to ThisDocument and delete the duplicate in the standard module.
The Macro Runs for Every Open Document
If you placed the DocumentBeforeSave event in the Normal.dotm template, the macro runs for every document that uses that template. To restrict the macro to one document, place the code only in that document’s ThisDocument module. Avoid editing Normal.dotm unless you intend the behavior globally.
The Macro Stops Working After Closing and Reopening the Document
This happens when the document was saved as a .docx file. Re-save the document as a Word Macro-Enabled Document (.docm). The event procedure is preserved only in macro-enabled files. Also check that the VBA project is not corrupted. Open the VBA editor, right-click the project, and select VBAProject Properties. On the Protection tab, ensure Lock project for viewing is unchecked if you want to edit the code later.
DocumentBeforeSave Event vs Other Save-Related Events
| Item | DocumentBeforeSave | DocumentSave |
|---|---|---|
| When it fires | Before the save operation begins | After the save operation completes |
| Can cancel the save | Yes, by setting Cancel = True | No, save has already finished |
| Location in VBA | ThisDocument module, Application or Document object | ThisDocument module, Document object only |
| Use case | Validation, logging, or backup before saving | Update metadata or trigger actions after the file is written |
If you need to run code after the save finishes, use the DocumentSave event instead. That event does not have a Cancel parameter because the save is already complete. Both events must be placed in the ThisDocument module of the specific document.
You can now schedule any VBA macro to run automatically when you save a Word document. Start by writing a simple test macro in the DocumentBeforeSave event of the target document. After verifying it works, replace the test code with your actual automation logic. To prevent accidental changes, protect the VBA project with a password via Tools > VBAProject Properties > Protection > Lock project for viewing.