我在 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
此代码在Workbook Open活动中添加了新的参考。当执行添加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