|启动Visual Basic编辑器。

在“项目”窗口中选择所需的项目。

通过选择菜单插入|插入新的类模块。类模块。

激活新的类模块并重命名,例如AppEventClass将这些示例宏复制并粘贴到新的类模块中:

Public WithEvents Appl As Application

Private Sub Appl_NewWorkbook(ByVal Wb As Workbook)

' your code here

MsgBox "A new workbook is created!"

End Sub

Private Sub Appl_WorkbookBeforeClose(ByVal Wb As Workbook, _

Cancel As Boolean)

' your code here

MsgBox "A workbook is closed!"

End Sub

Private Sub Appl_WorkbookBeforePrint(ByVal Wb As Workbook, _

Cancel As Boolean)

' your code here

MsgBox "A workbook is printed!"

End Sub

Private Sub Appl_WorkbookBeforeSave(ByVal Wb As Workbook, _

ByVal SaveAsUI As Boolean, Cancel As Boolean)

' your code here

MsgBox "A workbook is saved!"

End Sub

Private Sub Appl_WorkbookOpen(ByVal Wb As Workbook)

' your code here

MsgBox "A workbook is opened!"

End Sub

在为Application对象完成事件宏的编辑之后,您必须向ThisWorkbook模块中添加一些代码以激活新的事件宏:

Dim ApplicationClass As New AppEventClass

Private Sub Workbook_Open()

Set ApplicationClass.Appl = Application

End Sub

运行Workbook_Open过程后,将激活附加到Application对象的事件。