我正在尋找一種更好的方法來禁用在Excel工作簿中觸發的ActiveX事件(儘管這將適用於具有ActiveX對象的所有Office應用程序)。如何防止在VBA中觸發ActiveX事件?
希望類似於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
+ 1好問題!你讓我想了一會兒:) –
我的代碼是一個Excel的例子。 MS Word/PowerPoint的邏輯保持不變。不確定關於MS Access/Project/Outlook。 –