2012-05-10 39 views
6

我试过了3个不同的代码示例,它们都失败了。如何将菜单项添加到Excel 2010单元格上下文菜单 - 旧代码不起作用

下面是一个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年这个功能是否发生变化,我真的很感激吗?

回答

3

检查是否存在此类代码(无论是在您自己的插件还是您公司使用的任何其他插件中),以及是否将其注释掉或将其移至外挂程序的_Shutdown事件。

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

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

相关问题