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

Delete context menu options invoked from inside the context menu itself

我正在尝试删除 Excel 的上下文菜单中的一些自定义命令选项。我已将以下子项分配给上下文菜单按钮之一来完成这项工作,但是,当它要删除此按钮本身时,它无法执行此操作。我怎样才能做到这一点,或者是否有解决方法,例如将控制权释放给另一个 sub 等?

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

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

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 的函数