我尝试了 3 个不同的代码示例,但都失败了。
这是来自 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 年,我可能遗漏了什么,或者此功能是否发生了变化?
最佳答案
检查是否存在此类代码(在您自己的插件或您公司使用的任何其他插件中),如果确实存在,请将其注释掉或将其移至插件的 _Shutdown 事件。
//reset commandbars
Application.CommandBars["Cell"].Reset();
关于c# - 如何将菜单项添加到 Excel 2010 单元格上下文菜单 - 旧代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10528775/