VBA & Macro Problems

How to Cancel a Print Macro if Printer Setup is Canceled in Excel VBA

Khadija KhanKhadija Khan Nov 11, 2026 869 views

Question details

The user wants to stop an Excel VBA print macro from executing if the printer setup dialog box is canceled.

How to Cancel a Print Macro if Printer Setup is Canceled in Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in Excel.

2
Locate Your Print Macro

In the Project Explorer panel, find the module containing your existing print macro and double-click to open it.

3
Add the Conditional Statement

Insert the following code before your PrintOut command: If Not Application.Dialogs(xlDialogPrinterSetup).Show Then Exit Sub.

4
Complete the Macro

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.

Code Simplicity: This one-line condition is the most efficient way to handle dialog cancellations without needing complex error handling routines.

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. 1. Open your Macro Workbook in WPS: Launch WPS Spreadsheet and open your .xlsm file.
  2. 2. Enable Macros: Click 'Enable Macros' in the security warning bar at the top of the screen.
  3. 3. Access the VBA Editor: Go to the Developer tab and click 'Visual Basic' to edit your print macro code.
  4. 4. Run and Test: Run your macro. The printer setup dialog will appear, and your cancellation logic will execute perfectly.
High compatibility with Microsoft Excel VBA macros and scripts.Lightweight installation with a familiar, tabbed user interface.Built-in macro editor for easy debugging and code modification.Fully supports Excel formats including .xls, .xlsx, and .xlsm.
microsoft office alternative - wps office

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.