VBA & Macro Problems

How to Prevent Word Save and Save As from Bypassing Custom Macros

Ayan MasoodAyan Masood Nov 3, 2026 868 views

Question details

The user needs a way to enforce controlled file naming and PDF creation in a Word template without the default Save or Save As commands bypassing their custom macro logic.

How to Prevent Word Save and Save As from Bypassing Custom Macros
Product
Microsoft Word
Device & OS
not provided
Scenario
Developing a Word template with custom Save buttons designed to enforce controlled file generation and PDF creation.
Observed behavior
The built-in DocumentBeforeSave event interferes with template updates and PDF generation when users manually trigger Save or Save As actions.
Before you start

Ensure you have enabled the Developer tab in your Word ribbon and backed up your template file (.dotm) before modifying application-level VBA event handlers.

Solution 1Recommended

Use a Document Property Flag in the DocumentBeforeSave Event

Set a custom flag that your DocumentBeforeSave event checks to distinguish between user-initiated default saves and saves triggered explicitly by your custom macro button.

By assigning a document property as a flag, you can control the execution flow inside the DocumentBeforeSave event. When your custom Save button is clicked, it sets the flag and triggers the save method. The event handler will detect the flag, allow the save process, and then reset the flag for future operations. This method also allows you to seamlessly update SharePoint content-type properties before saving.

1
Create a Custom Document Property

In your Word template, go to File > Info > Properties > Advanced Properties. Switch to the Custom tab and add a Yes/No property named 'IsCustomSave', initializing it to 'No'.

2
Update the Custom Button Macro

Open the VBA Editor (Alt + F11). In your custom Save button's code, add logic to change the 'IsCustomSave' document property to 'Yes' before calling the ActiveDocument.Save or SaveAs2 method.

3
Modify the DocumentBeforeSave Event

Navigate to the ThisDocument module in your VBA project. If you haven't already, insert the DocumentBeforeSave event handler.

4
Check and Reset the Flag

Inside the BeforeSave event, write an IF statement that checks the 'IsCustomSave' property. If it evaluates to 'No', set Cancel = True to block the default save and optionally prompt the user to use the custom button. If it is 'Yes', allow the save to proceed and immediately reset 'IsCustomSave' back to 'No'.

Use a Document Property Flag in the DocumentBeforeSave Event
SharePoint Integration: You can insert additional code before resetting the flag to read field values and automatically update SharePoint content-type properties for seamless document management.
Advanced Document Control

Manage Macros and Custom Saves Seamlessly in WPS Writer

WPS Office provides robust VBA support in its premium versions, allowing you to seamlessly run your custom save macros and DocumentBeforeSave events with full compatibility to Microsoft Word templates.

  1. 1. Enable Developer Tools: Launch WPS Writer and navigate to the Developer tab to access the built-in VBA Editor.
  2. 2. Open Your Template: Load your macro-enabled template (.dotm or .docm) into the workspace.
  3. 3. Configure DocumentBeforeSave: Insert your custom logic into the ThisDocument module to intercept default save behaviors.
  4. 4. Execute Custom Macros: Run your custom Save macros to ensure controlled file generation and secure document compliance.
Fully compatible with Microsoft Word VBA macros (.docm, .dotm)Lightweight architecture for fast execution of custom scriptsBuilt-in Developer tools for editing VBA code seamlessly
microsoft office alternative - wps office

Frequently Asked Questions

What is the DocumentBeforeSave event in VBA?

It is an application-level or document-level event in VBA that triggers exactly before a document is saved. It allows developers to execute custom code, alter document properties, or cancel the save action entirely by setting the Cancel parameter to True.

How do I force users to use my custom save button instead of the default Save As dialog?

By putting a check in the DocumentBeforeSave event, you can set 'Cancel = True' if the user tries to save normally. You can then display a MsgBox directing them to click your custom button on the ribbon or document surface.

Can this macro update SharePoint properties before saving?

Yes. Inside the macro triggered by your custom save button, you can manipulate the ContentTypeProperties collection to update metadata fields based on document content before calling the ActiveDocument.SaveAs2 method to upload to the SharePoint site.

Why does my DocumentBeforeSave macro cause an endless loop?

An endless loop occurs if your DocumentBeforeSave event code calls ActiveDocument.Save without temporarily disabling events or using a flag. The save command inside the event triggers the event again. Using a custom property flag or setting Application.EnableEvents = False prevents this loop.