【问题标题】:How to add a menu item to Excel 2010 Cell Context Menu - old code doesn't work如何将菜单项添加到 Excel 2010 单元格上下文菜单 - 旧代码不起作用
【发布时间】:2012-05-10 06:47:30
【问题描述】:

我尝试了 3 种不同的代码示例,但都失败了。

这是来自 MSFT 员工 (How to show a context menu on a range) 的代码,其他两个示例的代码几乎完全相同:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    CommandBar cellbar = this.Application.CommandBars["Cell"];
    CommandBarButton button = (CommandBarButton) cellbar.FindControl(MsoControlType.msoControlButton, 0, "MYRIGHTCLICKMENU", Missing.Value, Missing.Value);
    if (button == null)
    {
        // add the button
        button = (CommandBarButton) cellbar.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, cellbar.Controls.Count, true);
        button.Caption = "Refresh";
        button.BeginGroup = true;
        button.Tag = "MYRIGHTCLICKMENU";
        button.Click += new _CommandBarButtonEvents_ClickEventHandler(MyButton_Click);
    }
}

private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool cancel)
{
    System.Windows.Forms.MessageBox.Show("MyButton was Clicked", "MyCOMAddin");
}

我希望在右键单击单元格时看到一个名为“刷新”的菜单项。然而运行上述代码(在 Excel 2010 中)没有“刷新”菜单项。

我可能遗漏了什么,或者此功能从 2007 年到 2010 年是否发生了变化?

【问题讨论】:

    标签: c# excel vsto contextmenu


    【解决方案1】:

    检查这种类型的代码是否存在(在您自己的插件或您公司使用的任何其他插件中),是否确实将其注释掉或将其移至插件的 _Shutdown 事件。

    //reset commandbars
    Application.CommandBars["Cell"].Reset();
    

    【讨论】:

    • 谢谢,正在加载另一个加载项,并且此重置代码正在删除我的菜单项
    【解决方案2】:

    这个问题太迟了,但可能对其他人有帮助......下面的代码对我来说很好。

    private void OnSheetRightClick(object Sh, Excel.Range Target, ref bool Cancel)
    {
        // code to create the menu item button
    }
    
    
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        this.Application.SheetBeforeRightClick += OnSheetRightClick;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 2014-06-03
      • 2012-07-25
      • 2011-09-21
      • 2017-04-23
      • 2010-10-20
      • 2013-12-27
      相关资源
      最近更新 更多