Before printing excel worksheet, disable if it was done by file menu and enable if by button on a sheet

nemke

New Member
Joined
Aug 23, 2022
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to resolve an issue with printing a sheet in Excel. So I was wondering whether there is a way to allow printing via button only. In ThisWorkbook I put the code below. What should I put in a module? How to pass 'button_used' variable from module back to ThisWorkbook?

Public Sub Workbook_BeforePrint(Cancel As Boolean)

'disables print if button is not used

If button_used = False Then MsgBox "Print is disabled, use button" Cancel = True End If End Sub
 

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).
So you already have a Sub associated with a button to start printing

The easiest way for doing what you need would be to toggle EnableEvents False/True and beginning/end of your print code
VBA Code:
Sub MyPrinting
Application.EnableEvents = False
'
'Your current code here
'
Application.EnableEvents = True
End Sub
This way your Sub Workbook_BeforePrint will not activate when you use the button

The problem with this code is that if the print routine does not complete (in case of run time error, for example) then EnableEvents could stay False, disabling Workbook_BeforePrint and any other event in the workbook.

So my suggestion is:
1) on top of a standard module of your vba project, declare Button_Used as a public variable:
VBA Code:
Public Button_Used As Boolean
Again: "On top of a vba standard module"

2) The modify your current print routine:
VBA Code:
Sub MyPrinting
Button_Used = True
'
'Your current code here
'
Button_Used = False
End Sub

3) modify your Workbook_BeforePrint as follows
VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Button_Used = False Then
    MsgBox "Print is disabled, use button"
    Cancel = True
Else
    Button_Used = False
End If
End Sub
This way Button_Used will be cleared at the first Workbook_BeforePrint (and Events will never get disabled)

Try...
 
Upvote 0
Solution
Many thanks Anthony for the reply.

It worked well :)

Best regards,
Nemanja
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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