2013-10-16 94 views
0

我需要檢測每個文件(* .xls,* .xlsx,* .xlsb,...)在保存之前在excel中打開許多屬性。Excel VBA:如何使全局事件Global_beforsave()

在* .xlsm文件中,我在此工作簿中包含Workbook_BeforeSave(),但僅適用於此工作簿。

我希望在* .xlam文件中包含一個全局事件,但我不知道該怎麼做。

回答

2

正如查爾斯鏈接顯示,你必須使用一個類模塊捕捉來自其他對象的事件的書面記錄。

我的建議是重用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 
1

看芯片皮爾遜對應用程序事件 here