我需要在保存之前检测在 excel 中打开的每个文件(*.xls、*.xlsx、*.xlsb、...)的许多属性。
在 *.xlsm 文件中,我在 thisWorkbook 中包含 Workbook_BeforeSave(),但仅适用于 thisWorkbook。
我希望在 *.xlam 文件中包含一个全局事件,但我不知道该怎么做。
正如 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
在此处查看 Chip Pearson 关于应用程序事件的文章