Excel 删除从上下文菜单本身内部调用的上下文菜单选项

Excel 删除从上下文菜单本身内部调用的上下文菜单选项,excel,vba,contextmenu,Excel,Vba,Contextmenu,我正在尝试删除Excel上下文菜单中的一些自定义命令选项。我已将以下子项指定给上下文菜单按钮之一来执行此操作,但是,当它删除此按钮本身时,它无法执行此操作。我如何实现这一点,或者是否有解决方法,例如将控件释放给另一个子节点等 Sub DeleteFromRightClickMenuOptions(sRightClickMenu As String) Dim ContextMenu As CommandBar Dim ctrl As CommandBarControl '

我正在尝试删除Excel上下文菜单中的一些自定义命令选项。我已将以下子项指定给上下文菜单按钮之一来执行此操作,但是,当它删除此按钮本身时,它无法执行此操作。我如何实现这一点,或者是否有解决方法,例如将控件释放给另一个子节点等

Sub DeleteFromRightClickMenuOptions(sRightClickMenu As String)
    Dim ContextMenu As CommandBar
    Dim ctrl As CommandBarControl

    ' Set ContextMenu to the Cell context menu.
    Set ContextMenu = Application.CommandBars(sRightClickMenu) 'instead of "List Range Popup", "Cell" can be used for regulat cells

    ' Delete the custom controls with the Tag : My_Tag.
    For Each ctrl In ContextMenu.Controls
        If ctrl.Tag = "My_Tag" Then
            ctrl.Delete
        End If
    Next ctrl
End Sub

最后,我找到了一个简单的解决方案:
Application.OnTime

上下文菜单中应分配给delete命令的函数应调用另一个函数,该函数将在一秒钟后运行主delete函数:

Sub DeleteFromRightClickMenuOptions_Main()
'since this sub is invoked by a context menu item and the item itself cannot be deleted we call another function and
'release the control this way
    On Error Resume Next
    Application.OnTime Now + TimeValue("00:00:01"), "RemoveContextMenuOptions"
End Sub
其中,
RemoveContextMenuOptions
是使用正确参数调用
DeleteFromRightClickMenuOptions
的函数