How to Use Word VBA to Confirm Before Closing a Document
Question details
The user wants to trigger a custom Yes/No confirmation dialog before closing a Word document using VBA.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Developing a custom VBA macro to control the document closing process and prevent accidental closures or unwanted save prompts.
- Observed behavior
- By default, Word closes normally or shows default save prompts. The user wants to intercept the close event with DocumentBeforeClose, show a custom prompt, and cancel the close operation if the user selects 'No'.
Ensure that your document is saved as a Macro-Enabled Document (.docm) and that you have enabled Developer tools and macros in your security settings.
Implement the DocumentBeforeClose Event using a Class Module
Set up a custom class module and a standard module to properly intercept the application-level close event and control the closing process.
To intercept the close event properly, you must use an Application-level event handler. This requires a Class module to define the event and a Standard module to initialize it upon opening the document.
Open the VBA Editor by pressing Alt + F11. Go to Insert > Class Module. In the Properties window, rename the module to 'clsEvents'. Inside this module, declare the application variable by typing: Public WithEvents App As Word.Application.
In 'clsEvents', select 'App' from the left dropdown menu and 'DocumentBeforeClose' from the right. Add a MsgBox for Yes/No. If the user clicks No, set the command 'Cancel = True' to abort the closing process.
Go to Insert > Module to create a standard module. Declare a public instance of your class by typing: Public evt As New clsEvents.
In the standard module, create 'AutoOpen' and 'AutoNew' subroutines. Inside them, initialize the class by adding: Set evt.App = Word.Application. Ensure you remove any 'Stop' statements from your code, as they act as breakpoints and will pause VBA execution.
To cleanly release memory, create an 'AutoClose' subroutine in the standard module and add the line: Set evt = Nothing.

Manage Macros Easily with WPS Writer
WPS Office fully supports VBA macros, allowing you to run custom scripts like DocumentBeforeClose directly in WPS Writer without complex configurations.
- 1. Open WPS Writer: Launch the WPS Office application and open your document.
- 2. Enable Developer Tools: Navigate to the Developer tab on the top ribbon to access the Macro settings.
- 3. Launch the VBA Editor: Click on 'Visual Basic' or press Alt + F11 to open the integrated VBA environment.
- 4. Insert your Code: Follow the standard VBA module structure to insert your Class and Standard modules for custom close prompts.

Frequently Asked Questions
Why does Word still prompt to save when I select No in my custom dialog?
This occurs if the 'Cancel = True' command is not executed properly or if the DocumentBeforeClose event wasn't successfully hooked. Make sure your Event class is correctly initialized in the AutoOpen routine.
What does the Stop statement do in VBA?
The 'Stop' statement acts like a manual breakpoint. It pauses code execution and brings up the VBA editor. You must remove it from your final macro code so the script runs seamlessly without interruption.
Can I apply this macro to a specific document only?
Yes. By saving the macro directly in the specific document or its attached custom template (rather than the global Normal template), the custom close confirmation will only trigger for that particular file.




