【问题标题】:Delete context menu options invoked from inside the context menu itself删除从上下文菜单本身内部调用的上下文菜单选项
【发布时间】:2020-03-14 11:20:25
【问题描述】:

我正在尝试删除 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

【问题讨论】:

    标签: excel vba contextmenu


    【解决方案1】:

    最后,我找到了一个简单的解决方案: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 的函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多