c# - 如何将菜单项添加到 Excel 2010 单元格上下文菜单 - 旧代码不起作用

标签 c# excel vsto contextmenu

我尝试了 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/

相关文章:

php - 如何使用PHP解析Excel文件

C# 格式化文本(右对齐)

c# - 认识 WCF 解决方案

c# - 在 ConfigureServices 中注入(inject)依赖

c# - ASP.NET Core 2.2 中的 Azure AD 身份验证

扩展行的 VBA 动态范围

带取消功能的VBA密码输入

.net - 获取 Outlook.PropertyPageSite,以便您可以调用 OnStatusChange() 并更新 UI

c# - 在 asp.net 中创建 Excel 工作簿

c# - 如何将新的 TextRange.Run 添加到 PowerPoint 文本框?