Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...