How to add a menu item to Excel 2010 Cell Context Menu - old code doesn't work

若如初见. 提交于 2019-12-30 06:40:10

问题


I've tried 3 different code samples and they all fail.

Here's the code from a MSFT employee (How to show a context menu on a range), the other two samples have pretty much the exact same code:

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

I'm expecting to see a menu item called Refresh when right clicking on a cell. Yet running the above code (in Excel 2010) there is no 'Refresh' menu item.

What might I be missing, or did this functionality change from 2007 to 2010?


回答1:


Check to see if this type of code exists (either in your own addin or any other addin's your company uses) and if it does either comment it out or move it to the _Shutdown event of the addin.

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


来源:https://stackoverflow.com/questions/10528775/how-to-add-a-menu-item-to-excel-2010-cell-context-menu-old-code-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!