c# - 自定义 Excel 右键单击​​上下文菜单被快速分析默认值覆盖

标签 c# excel vsto

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

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

enter image description here

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

enter image description here

我想这与干扰我的自定义上下文菜单覆盖的快速分析功能有关。这是我在 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。)

最佳答案

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

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

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

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

关于c# - 自定义 Excel 右键单击​​上下文菜单被快速分析默认值覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668109/

相关文章:

c# - 泛型参数的属性有什么用?

html - seleniumBasic FindElementByXPath 内部文本

excel - 删除值为 '#NAME?' 或引用 '=#NAME?' 的命名范围

Outlook 2003 插件

c# - 无法修改存储在另一个属性中的结构的属性

C# 错误 - "Not all code paths return a value"

vba - 如何在 Excel VBA 中仅迭代自动筛选工作表中的行?

c# - 查找 VSTO 日志

c# - 在将图像用作 PictureBox 的源后,如何从文件夹中删除这些图像?

c# - 多个正则表达式检查相同的字符串,没有重叠的结果,同时忽略单词