1

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
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
dorado
  • 11
  • 1
  • You could use a Global `AllowPrint` flag which you set to True set in the code attached to the "print" button - if that's not set in the `Workbook_BeforePrint` method then set `Cancel = True`. Don't forget to set back to False once printing is done. – Tim Williams Aug 22 '22 at 21:52
  • And how the code should be? I declared a global variable in private module via Public keyword, but I can not access it in ThisWorkbook module – dorado Aug 23 '22 at 09:12
  • Should be fine if you declare it in a regular (non-private) module. – Tim Williams Aug 23 '22 at 15:47
  • Many thanks Tim, everything turned up great :) – dorado Aug 25 '22 at 09:43

0 Answers0