正如Charles链接所示,您必须使用类模块来捕获来自其他对象的事件。
我的建议是重复使用
ThisWorkbook
对象:
'App variable which should be assigned to current Application instance:
Public WithEvents App As Excel.Application
'Use open event on your workbook to assign App
Private Sub Workbook_Open()
Set App = Application
End Sub
'Code catching WorkbookBeforeSave event on App
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
Debug.Print "Workbook "; Wb.Name; " is about to be saved..."
End Sub