我正在寻找一种更好的方法来禁用 ActiveX 事件在 Excel 工作簿中触发(尽管这适用于所有具有 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
我知道这真的很老了。但任何查找此内容的人(第一次在谷歌上点击)可能想要一个简单的答案:
假设您有一个 Private Sub ActiveXControl_Change() 在 Application.EnableEvents = False 期间被调用,并且您希望它跳过此操作:
Private Sub ActiveXControl_Change()
If Application.EnableEvents = True Then
'enter you code here
End If
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)