How to Automatically Save an Excel Workbook Before Closing Using VBA
Question details
The user wants to automatically save an Excel workbook upon closing to suppress the default save confirmation prompt, specifically to automate processes via Task Scheduler.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating workbook operations using Windows Task Scheduler where an unattended workbook needs to run a macro and close without halting for a save prompt.
- Observed behavior
- Excel halts the automated closing process by displaying a save confirmation dialog, which interrupts unattended macro executions like PDF generation.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that macros are allowed to run in your Trust Center settings.
Use ThisWorkbook.Save to Automatically Save and Close
Use this method to save the workbook silently and suppress the confirmation dialog, which is ideal for unattended automation.
By adding a simple save command to the Workbook_BeforeClose event, Excel will save any changes automatically before the application closes. This allows existing scripts, such as PDF generation, to finish first.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left side, double-click on 'ThisWorkbook'.
From the left dropdown at the top of the code window, select 'Workbook'. From the right dropdown, select 'BeforeClose'.
Type 'ThisWorkbook.Save' immediately before the 'End Sub' line. Make sure it comes after any other automated code you have.

Close Without Saving Changes (Suppress Prompt)
Use this method if you want to discard any background changes (like VLOOKUP updates) without triggering the save prompt.
Automate Tasks with VBA in WPS Spreadsheets
WPS Office Spreadsheets provides robust support for VBA macros. You can run automated scripts, handle background tasks like generating PDFs, and utilize the Workbook_BeforeClose event seamlessly.
- 1. Enable Developer Tools: Open WPS Spreadsheets and navigate to the 'Developer' tab on the top ribbon.
- 2. Open the VBA Editor: Click on 'Visual Basic' or press ALT + F11 to launch the script editor.
- 3. Apply the Macro: Double-click 'ThisWorkbook' in the Project Explorer, paste your Workbook_BeforeClose code, and save the document as a macro-enabled workbook.

Frequently Asked Questions
Why is SendKeys not recommended for handling the save prompt?
SendKeys relies on the exact timing of the dialog box appearing. If system lag delays the prompt, the key commands may be sent before the dialog appears, causing the script to fail or behave unpredictably.
Where exactly do I paste the Workbook_BeforeClose code?
You must place the code strictly in the 'ThisWorkbook' module within the VBA Editor. It will not trigger if placed in a standard module (like Module1) or a specific worksheet module (like Sheet1).
Will this code work if I run the file unattended through Windows Task Scheduler?
Yes, by suppressing the save prompt with ThisWorkbook.Save, the workbook is able to close silently. This prevents the application from hanging, making it perfect for unattended automation via Task Scheduler.




