Home » excel » excel – How to capture the "Print" button from the menu bar in a macro

excel – How to capture the "Print" button from the menu bar in a macro

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a sheet with a custom button on it from where I control the printing process.

Now the user clicks on the menu bar’s print icon and this produces an “undefined” output.

How can I intercept this menu bar button?

  • Thanks
How to&Answers:

Handle the Workbook_BeforePrint event.

private sub Workbook_BeforePrint (cancel as boolean)
  '//g_MyFlag is set when the user clicks you toolbar button.
  '//It must get cleared in the end of your procedure.
  if not g_MyFlag then cancel = true: exit sub

end sub

In MS Word, it’s also possible to redefine the system macro itself. You’d have to create a macro named FilePrint(), and Word would call it instead its own. A pity you can’t do that in Excel.