【问题标题】:Custom excel right click context menu gets overwritten by Quick Analysis defaults自定义 excel 右键单击​​上下文菜单被快速分析默认值覆盖
【发布时间】:2014-03-26 17:02:20
【问题描述】:

我正在关注this example 为带有 VSTO 的 excel 加载项创建自定义右键单击自定义菜单,并在特定条件下显示它(在 Excel 命名表的范围内单击鼠标右键)。

当我在命名表范围之外右键单击时,我修改后的示例代码版本就像一个魅力:

但是当您在命名的表格范围内单击鼠标右键时它不会显示:

我想这与干扰我的自定义上下文菜单覆盖的快速分析功能有关。这是我在 ThisAddin.cs 中使用的代码:

 void Application_SheetBeforeRightClick(object worksheet, Excel.Range range, ref bool cancel)
{
  GetCellContextMenu().Reset(); // reset the cell context menu back to the default
  // If the selected range belongs within a named excel table we display the refresh menu item at the right click context menu.
  if (true) //range.IntersectsWithAnyExcelTable()) <-- this code works fine but I commented it out for the purpose of showing the problem (in this case the custom popup meny should appear ALWAYS):
  {
    const OfficeCore.MsoControlType menuItem = OfficeCore.MsoControlType.msoControlButton;
    var refreshMenuItem = (OfficeCore.CommandBarButton)GetCellContextMenu().Controls.Add(menuItem, missing, missing, 1, true);// where missing = global::System.Type.Missing;
        refreshMenuItem.Style = OfficeCore.MsoButtonStyle.msoButtonCaption;
        refreshMenuItem.Caption = "Refresh My Data";
        refreshMenuItem.Click -= RefreshMenuItemClick;
        refreshMenuItem.Click += RefreshMenuItemClick;
      }
    }

并且不要忘记在加载项启动时订阅事件:

Application.SheetBeforeRightClick += Application_SheetBeforeRightClick;

我该怎么做:

  1. 尽管启动了快速分析,但仍显示我的自定义菜单。

  2. 覆盖快速分析刷新按钮功能(我是impossible。)

【问题讨论】:

    标签: c# excel vsto


    【解决方案1】:

    Excel 为表格使用单独的右键单击菜单。

    我只会说 VBA,所以你必须翻译...

    在“普通”单元格中右键单击时,将使用CommandBars("Cell") 菜单。

    在表格中单击鼠标右键时,将使用CommandBars("List Range Popup") 菜单。

    【讨论】:

    • 在调试了一段时间后,我们得出了与您在上面发布的相同的结论......问题在于,有 158 个弹出菜单。您先生为我们节省了很多时间来确定哪个是适合这种情况的!
    • +1 为答案。根据meta.stackexchange.com/questions/5029/…删除签名
    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2010-10-16
    • 2011-11-13
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多