Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel (97) macro to close and not save file when printed 1

Status
Not open for further replies.

lsgko

Programmer
Sep 9, 2002
111
US
I am wondering if it is possible to make a macro in an excel file that upon the user printing the file, it closes excel and if the user is prompted to save the macro automatically pushes no.

If this is possible, how would it be done?

 
You could either cause Excel to Quit or the workbook to close. Either way, the code needs to go in a Workbook_BeforePrint sub in the ThisWorkbook code pane.
Code:
Private Sub Private Sub Workbook_BeforePrint(Cancel As Boolean)
Application.DisplayAlerts = False
'ThisWorkbook.Close savechanges:=False    'I get an error with this statement in Excel 97
Application.Quit    'This statement works fine
End Sub
As noted in the code comments, I got a fatal error when trying to close the workbook programmatically (the workbook closed, but Excel then crashed).
 
That commented line caused excel to crash on my machines too. After commenting it out, it works perfectly.
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top