如何将菜单项添加到 Excel 2010 单元格上下文菜单 - 旧代码不起作用
本文关键字:菜单 上下文 不起作用 代码 单元格 2010 菜单项 添加 Excel | 更新日期: 2023-09-27 18:33:47
我尝试了 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)
{
// 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();
这个问题为时已晚,但可能会帮助其他人......下面的代码对我来说很好用。
private void OnSheetRightClick(object Sh, Excel.Range Target, ref bool Cancel)
{
// code to create the menu item button
}
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
this.Application.SheetBeforeRightClick += OnSheetRightClick;
}