如何将菜单项添加到Excel 2010单元格上下文菜单 - 旧代码不起作用 [英] How to add a menu item to Excel 2010 Cell Context Menu - old code doesn't work

查看:162
本文介绍了如何将菜单项添加到Excel 2010单元格上下文菜单 - 旧代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了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)
{
//添加按钮
button =(CommandBarButton)cellbar.Controls.Add(MsoControlType.msoControlButton,Missing.Value,Missing。 Value,cellbar.Controls.Count,true);
button.Caption =刷新;
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被点击,MyCOMAddin);
}

我期待看到一个名为Refresh的菜单项点击一个单元格。然而,运行上述代码(在Excel 2010中)没有刷新菜单项。



真的很感激任何关于我可能丢失的提示或者如果这个功能从2007年改为2010年?

解决方案

检查这种类型的代码是否存在(在您自己的加载项中或您公司使用的任何其他加载项),如果它发表任何评论,或将其移动到Addin的_Shutdown事件。

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


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.

Would really appreciate any tips as to what I might be missing or if this functionality changed from 2007 to 2010?

解决方案

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

这篇关于如何将菜单项添加到Excel 2010单元格上下文菜单 - 旧代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆