VBA & Macro Problems

Cancel Excel Workbook Printing with the VBA BeforePrint Event

Nimra MalikNimra Malik Nov 14, 2026 868 views

Question details

Prevent a workbook from printing when the user selects Cancel in a VBA confirmation dialog.

Cancel Excel Workbook Printing with the VBA BeforePrint Event
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A workbook uses the Workbook_BeforePrint event to ask the user for confirmation before printing.
Observed behavior
Printing can continue when the procedure exits without setting the event's Cancel argument to True.
Before you start

Open the workbook in desktop Excel and make sure macros are enabled for the file. The Workbook_BeforePrint event belongs to Excel, not Word; Word uses a different print event.

Solution 1Recommended

Set the BeforePrint Cancel Argument from the Dialog Result

Use this event procedure to cancel printing when the user clicks Cancel and allow it to continue when the user clicks OK.

1
Open the VBA editor

In Excel, press Alt+F11 or select Developer > Visual Basic.

2
Open the workbook module

In the VBA editor's Project Explorer, expand your workbook project and double-click ThisWorkbook.

3
Add the print event

Add or update the procedure in ThisWorkbook: Private Sub Workbook_BeforePrint(Cancel As Boolean) Cancel = (MsgBox("Always 2 Copies and Sorted", vbOKCancel, "Caution") = vbCancel) End Sub

4
Save and test

Save the file as an Excel Macro-Enabled Workbook (.xlsm), then choose File > Print. Click Cancel in the message box and verify that printing stops; repeat and click OK to allow printing.

Set the BeforePrint Cancel Argument from the Dialog Result
The message does not set print options: The example displays a reminder about two sorted copies, but it does not configure the number of copies or collation.
Free Microsoft Office alternative

Work with Spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening, editing, and printing spreadsheets. Its format compatibility makes moving everyday documents easier, but it does not fix Excel's VBA event behavior; test any macros before relying on them.

Open and print spreadsheets using a familiar interfaceCompatible with Microsoft Excel .xlsx and Word .docx formatsFree and lightweight for everyday office tasksMove common Office documents to WPS without starting from scratch
microsoft office alternative - wps office

Frequently Asked Questions

Why does printing continue after I click Cancel?

Exiting the event procedure alone does not cancel printing. In ThisWorkbook, set the Workbook_BeforePrint event's Cancel argument to True when the dialog returns vbCancel.

Where should I put the Workbook_BeforePrint code?

In Excel, open Developer > Visual Basic, then double-click ThisWorkbook under your workbook project and add the event procedure there.

Why doesn't the confirmation dialog appear?

Check that the file is saved as .xlsm, macros are enabled, and the procedure is in ThisWorkbook. Also check whether events have been disabled; in the VBA editor's Immediate window, you can enter Application.EnableEvents = True.

Can I use Workbook_BeforePrint in Microsoft Word?

No. Workbook_BeforePrint is an Excel workbook event. Word uses a different application-level print event, DocumentBeforePrint, which also provides a Cancel argument.