How to Cancel a Print Macro if Printer Setup is Canceled in Excel VBA
Question details
The user wants to stop an Excel VBA print macro from executing if the printer setup dialog box is canceled.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Creating or modifying a VBA print macro where user interaction with the printer selection dialog dictates whether the print job proceeds.
- Observed behavior
- Currently, the print macro continues executing and prints the document even after the user clicks 'Cancel' in the printer setup dialog.
Ensure you have the Developer tab enabled in Excel and that your workbook is saved as a Macro-Enabled Workbook (.xlsm) before modifying the VBA code.
Use Dialog Return Value to Cancel Print Job
The cleanest method is to evaluate the Boolean return value of the printer setup dialog. If it returns False (canceled), the macro exits immediately.
The Application.Dialogs(xlDialogPrinterSetup).Show method returns True if the user clicks OK, and False if the user clicks Cancel. By checking this value in an If statement, you can safely exit the subroutine before reaching the PrintOut command.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in Excel.
In the Project Explorer panel, find the module containing your existing print macro and double-click to open it.
Insert the following code before your PrintOut command: If Not Application.Dialogs(xlDialogPrinterSetup).Show Then Exit Sub.
Ensure your ActiveWindow.SelectedSheets.PrintOut command comes after the conditional statement. Run the macro to test if canceling the dialog successfully stops the print job.
Use Error Handling to Detect Cancellation
Alternatively, you can use On Error Resume Next to catch errors thrown if the dialog process is interrupted or fails.
Run and Edit VBA Macros Seamlessly in WPS Office
WPS Office Spreadsheet provides excellent compatibility with Excel macros, allowing you to write, edit, and run VBA code—including custom print dialog handlers—just as you would in Microsoft Excel.
- 1. Open your Macro Workbook in WPS: Launch WPS Spreadsheet and open your .xlsm file.
- 2. Enable Macros: Click 'Enable Macros' in the security warning bar at the top of the screen.
- 3. Access the VBA Editor: Go to the Developer tab and click 'Visual Basic' to edit your print macro code.
- 4. Run and Test: Run your macro. The printer setup dialog will appear, and your cancellation logic will execute perfectly.

Frequently Asked Questions
Why does my Excel macro print even if I hit Cancel in the printer dialog?
By default, calling Application.Dialogs(...).Show only displays the dialog. If you don't explicitly check its return value, the VBA code will simply proceed to the next line, which is usually the PrintOut command, resulting in unwanted printing.
What does the xlDialogPrinterSetup constant do in VBA?
The xlDialogPrinterSetup constant triggers the built-in Excel Printer Setup dialog box, allowing the user to select a printer and adjust properties before a document is printed.
Can I use this same logic for other Excel dialog boxes?
Yes. Most built-in dialogs invoked via Application.Dialogs(xlDialog...).Show return a Boolean value (True for OK, False for Cancel). You can use the same If Not ... Then Exit Sub logic to handle cancellations for saving, opening, or formatting dialogs.




