Unable to Disable Worksheet Printing

whereswayno

New Member
Joined
Aug 7, 2011
Messages
3
This is very frustrating. I am using a Windows 7 machine with Excel 2010. I want to cancel printing a worksheet if there are too many pages. Thus I understand that I need to have code as follows:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If PageSetup.Pages > 2 then
Cancel = True
MsgBox "Please change the print area into ONLY 2 pages.", vbInformation
End If
End Sub

However, none of this code is being run when the print icon is pressed or when someone clicks File -> Print -> Print. I went to the trust center and verified that all macros are enabled and that access to the VBA project module was trusted too. I verified that I saved the project before trying to click the print icon.

In case my code was incorrect, I simplified it:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
MsgBox "Please change the print area into ONLY 2 pages.", vbInformation
End Sub

I never received this message, so none of it ran.

Does anyone have any ideas how to fix this?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Are the events enabled? Run once:

Code:
Application.EnableEvents = True

Also, where did you paste the code?
 
Upvote 0
I added your code as the first line inside the Sub, but that did not change the result. I put my code in Sheet2 (once I can get this code to work in this sheet, I'll be putting similar code in the other sheets).
 
Upvote 0
Code:
Private Sub [COLOR=red][B]Workbook[/B][/COLOR]_BeforePrint
It's a workbook event; it needs to go in ThisWorkbook.
 
Upvote 0
Try this:

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    If ActiveSheet.PageSetup.Pages.Count > 2 Then
        MsgBox "Please change the print area into ONLY 2 pages.", vbInformation
        Cancel = True
    Else
        MsgBox "Okey-dokey"
    End If
End Sub
If nothing happens, make sure that the code is in the ThisWorkbook module, macros are enabled, and that

? Application.EnableEvents

entered in the Immediate window returns True.
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top