1

我只想在所有工作簿(有或没有宏)中捕获打开和保存事件。据我了解,我需要带有启用 Event 的代码的 AddIn。我在互联网上找到了很多例子。我学习了 stackoverflow 示例,如下所示:Excel Add-In 如何响应任何工作表中的事件? 但它只是不起作用。没有人报告任何问题。所以,我想我在这里犯了一个简单的错误。我有下一个插件,例如:

类模块 (ExcelEventCapture) - 私有:

Option Explicit
Public WithEvents ExcelApp As Excel.Application

Private Sub Class_Initialize()
Set ExcelApp = New Excel.Application
ExcelApp.EnableEvents = True
MsgBox "ExcelApp OK"
End Sub

Private Sub ExcelApp_NewWorkbook(ByVal Wb As Workbook)
Wb.Close savechanges:=False
MsgBox "Sorry - you can't create workbooks in this system!"
End Sub

此代码放置在 ThisWorkbook 中:

Option Explicit
Private ExcelEvents As ExcelEventCapture
Private Sub Workbook_Open()
Set ExcelEvents = New ExcelEventCapture
MsgBox "ExcelEvents OK"
End Sub

每次保存/修改此代码后,我都会使用插件管理器来关闭/打开此插件。打开后,我看到“ExcelApp OK”、“ExcelEvents OK”。但是当我创建新工作簿时,不会发生任何事件。其他事件也不处理。我用 2007/2010 Excel 试试。结果一样。请帮忙。

4

1 回答 1

2

您的代码不太正确:

类模块 (ExcelEventCapture)

Option Explicit

Public WithEvents ExcelApp As Application

Private Sub ExcelApp_NewWorkbook(ByVal Wb As Workbook)
    Wb.Close savechanges:=False
    MsgBox "Sorry - you can't create workbooks in this system!"
End Sub

此代码放置在 ThisWorkbook 中:

Option Explicit

Private ExcelEvents As New ExcelEventCapture

Private Sub Workbook_Open()
    Set ExcelEvents.ExcelApp = Application
    MsgBox "ExcelEvents OK"
End Sub
于 2012-05-06T00:46:08.770 回答