C# 如何将菜单项添加到Excel 2010单元格上下文菜单-旧代码不';行不通

C# 如何将菜单项添加到Excel 2010单元格上下文菜单-旧代码不';行不通,c#,excel,vsto,contextmenu,C#,Excel,Vsto,Contextmenu,我尝试了3个不同的代码示例,但都失败了 以下是来自MSFT员工()的代码,其他两个示例的代码几乎完全相同: private void ThisAddIn_Startup(object sender, System.EventArgs e) { CommandBar cellbar = this.Application.CommandBars["Cell"]; CommandBarButton button = (CommandBarButton) cellbar.FindCont

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

以下是来自MSFT员工()的代码,其他两个示例的代码几乎完全相同:

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年,此功能是否发生了变化?

检查此类型的代码是否存在(在您自己的加载项或您公司使用的任何其他加载项中),以及是否对其进行了注释或将其移到加载项的_Shutdown事件中

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

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

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;
}

谢谢,另一个加载项正在加载,其重置代码正在删除我的菜单项