当添加新的引用时,excel停止触发事件
我在ThisWorkbook中有这样的代码:
Public WithEvents App As Application Private Sub Workbook_Open() Application.EnableEvents = True ' it is a simple control RefEdit, but it could be anything: ThisWorkbook.VBProject.References.AddFromGuid{00024517-0000-0000-C000-000000000046},0,0 Set App = Application End Sub Private Sub App_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Debug.Print "Selection changed" End Sub
此代码在工作簿打开事件中添加新的参考。 当执行从GUID添加引用的行时,excel停止触发SheetSelectionChange
事件。 以及其他事件。 如果我评论这一行,excel成功启动所有事件。 你能告诉我我做错了什么吗? 调用函数来添加项目中的新引用是否好的地方?
显然事件和添加一个新的参考不相互爱 – 参考 。
这是围绕它的方式,在那里提出:
Private Sub Workbook_Open() ' 'Must have reference to the VBE IDE to access reference information ' ' Hardcode adding the GUID for the VBE IDE... ThisWorkbook.VBProject.References.AddFromguid "{00-000000000046}", 0, 0 Application.OnTime Now() + TimeValue("00:00:01"), "SetupApplicationEvents" End Sub
在一个模块中:
Sub SetupApplicationEvents Set App = Application End Sub