我正在寻找一种更好的方法来禁用在 Excel 工作簿中触发 ActiveX 事件(尽管这将适用于所有具有 ActiveX 对象的 Office 应用程序)。
希望类似于 的东西Application.EnableEvents = false
,尽管这不适用于 ActiveX。
在下面的示例中,使用全局布尔值很简单,但我的 ActiveX 对象有很多事件处理程序,而且对于我可以普遍应用的东西来临时禁用 ActiveX 事件会非常容易。我真的不想为这些方法中的每一个添加一个 if/exit 子语句。
要演示此问题,请在工作表中创建一个 ActiveX 组合框并将以下内容添加到该工作表模块
Public initializingContent As Boolean
Private Sub intializeAllActiveXContent()
'this doesn't apply to activeX events :'(
Application.EnableEvents = False
'this could work but is not really elegant
'change this to false to show my problem in
'the intermediate window (called not once but twice)
initializingContent = True
ComboBoxTest.Clear
ComboBoxTest.AddItem ("item1")
ComboBoxTest.AddItem ("item2")
ComboBoxTest.AddItem ("item3")
'select the top value in the box
ComboBoxTest.value = "item1"
initializingContent = False
Application.EnableEvents = True
End Sub
Private Sub ComboBoxTest_Change()
'I really don't want to have to wrap EVERY single ActiveX method
'with something like this for a whole variety of reasons...
If initializingContent Then Exit Sub
Debug.Print "do stuff I don't want to happen when intializeAllActiveXContent() runs " & _
"but I do when user changes box"
End Sub