logo
search
Office Settings & Configuration

How to Use Word VBA to Confirm Before Closing a Document

Tauseeq MagsiTauseeq Magsi Oct 15, 2026 869 views

Question details

The user wants to trigger a custom Yes/No confirmation dialog before closing a Word document using VBA.

How to Use Word VBA to Confirm Before Closing a Document
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'.
Before you start

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.

Solution 1Recommended

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.

1
Create a new Class Module

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.

2
Write the event handler

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.

3
Set up the Standard Module

Go to Insert > Module to create a standard module. Declare a public instance of your class by typing: Public evt As New clsEvents.

4
Initialize the event

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.

5
Clear the object on close

To cleanly release memory, create an 'AutoClose' subroutine in the standard module and add the line: Set evt = Nothing.

Implement the DocumentBeforeClose Event using a Class Module
Avoid Extra Save Prompts: Setting Cancel = True directly within the event prevents both the document from closing and the default 'Do you want to save changes' prompt from appearing when you choose 'No'.
Advanced Document Control

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. 1. Open WPS Writer: Launch the WPS Office application and open your document.
  2. 2. Enable Developer Tools: Navigate to the Developer tab on the top ribbon to access the Macro settings.
  3. 3. Launch the VBA Editor: Click on 'Visual Basic' or press Alt + F11 to open the integrated VBA environment.
  4. 4. Insert your Code: Follow the standard VBA module structure to insert your Class and Standard modules for custom close prompts.
Fully compatible with Microsoft Word VBA macros and .docm file formats.Lightweight architecture runs custom scripts efficiently and smoothly.Free access to powerful developer tools and document editing capabilities.
microsoft office alternative - wps office

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.