Microsoft Office Tutorials and References
In Depth Information
Getting Acquainted with Workbook-Level Events
When the user attempts to save the workbook, the Workbook_BeforeSave procedure is
executed. If the save operation will bring up Excel’s Save As dialog box, the SaveAsUI variable is
True . The Workbook_BeforeSave procedure checks this variable and displays a message only
if the Save As dialog box will be displayed. If the procedure sets the Cancel argument to True ,
the file won’t be saved (or the Save As dialog box won’t be shown).
The Deactivate event
The following example demonstrates the Deactivate event. This procedure is executed
whenever the workbook is deactivated, and essentially never lets the user deactivate the workbook.
One way to trigger the Deactivate event is to activate a different workbook window. When
the Deactivate event occurs, the code reactivates the workbook and displays a message.
Private Sub Workbook_Deactivate()
MsgBox “Sorry, you may not leave this workbook”
End Sub
I don’t recommend using procedures, such as this one, that attempt to “take over”
Excel. It can be very frustrating and confusing for the user. Rather, I would recommend
training the user how to use your application correctly.
This example also illustrates the importance of understanding event sequences. If you try out this
procedure, you’ll see that it works well if the user attempts to activate another workbook.
However, it’s important to understand that the workbook Deactivate event is also triggered
by the following actions:
h Closing the workbook
h Opening a new workbook
h Minimizing the workbook
In other words, this procedure may not perform as it was originally intended. When programming
event procedures, you need to make sure that you understand all the actions that can trigger the
The BeforePrint event
The BeforePrint event occurs when the user requests a print or a print preview but before the
printing or previewing actually occurs. The event uses a Cancel argument, so your code can
cancel the printing or previewing by setting the Cancel variable to True . Unfortunately, you
can’t determine whether the BeforePrint event was triggered by a print request or by a
preview request.
Search JabSto ::

Custom Search