【问题标题】:Deleting CommandBarButtons from the Application.CommandBars["Cell"] isn't removing them from all open workbooks on ThisAddIn_Shutdown event从 Application.CommandBars["Cell"] 中删除 CommandBarButtons 不会在 ThisAddIn_Shutdown 事件中从所有打开的工作簿中删除它们
【发布时间】:2014-12-08 22:51:57
【问题描述】:

我有一个带有 .NET 4.0 的 C# VSTO 应用程序,它使用 Globals.ThisAddIn.Application.CommandBars["Cell"] 上下文菜单中的两个自定义 CommandBarButtons。我在 ThisAddIn_Startup 事件上创建了一次按钮,它们对所有工作簿都非常有效。如果我的加载项在某个时候关闭而没有关闭 Excel,那么如果打开的工作簿超过 1 个,则会出现问题。只有活动工作簿的右键上下文菜单才会删除按钮。

代码:

public partial class ThisAddIn
{
    private const string TAG_PASTE_EVENT = "PASTE EVENT";
    private const string TAG_COPY_EVENT = "COPY EVENT";
    private Office.CommandBarButton copy_event, paste_event;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        try
        {
            DefineShortcutMenu();

            /*
             * Other start up stuff that works fine
             */
        }
        catch (Exception ex)
        {
            ExceptionHandler.HandleException(ex);
        }
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        /*
         * Other shut down stuff that works fine 
         */

        // Remove the right click menu buttons
        foreach (Office.CommandBarControl control in Application.CommandBars["Cell"].Controls)
        {
            if (control.Tag.Equals(TAG_PASTE_EVENT))
            {
                control.Delete(true);
            }
            else if (control.Tag.Equals(TAG_COPY_EVENT))
            {
                control.Delete(true);
            }
        }
    }

    private void copy_event_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
    {
        // Copy code
    }

    private void paste_event_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
    {
        // Paste code
    }

    private void DefineShortcutMenu()
    {
        // Create and add the paste button
        paste_event = (Office.CommandBarButton)Application.CommandBars["Cell"].Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, true);
        paste_event.Style = Office.MsoButtonStyle.msoButtonCaption;
        paste_event.Caption = "Paste Event";
        paste_event.Tag = TAG_PASTE_EVENT;
        paste_event.DescriptionText = "Stuff happens";
        paste_event.Enabled = false;
        paste_event.Click += paste_event_Click;

        // Create and add the copy button
        copy_event = (Office.CommandBarButton)Application.CommandBars["Cell"].Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, true);
        copy_event.Style = Office.MsoButtonStyle.msoButtonCaption;
        copy_event.Caption = "Copy Event";
        copy_event.Tag = TAG_COPY_EVENT;
        copy_event.DescriptionText = "Stuff happens";
        copy_event.Enabled = false;
        copy_event.Click += copy_event_Click;
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
}

我正在使用 Excel 2013,我知道它有一个与右键单击上下文菜单相关的错误(这就是为什么我对 CommandBarControls 使用 foreach 而不是使用全局变量的原因)。非常感谢您发现的任何工作流程!

澄清:一切正常,唯一的问题是如果加载项关闭,CommandBarButtons 不会从非活动工作簿的右键单击上下文菜单中删除。如果在同一会话期间重新打开加载项,所有工作簿都会再次获得复制和粘贴按钮,这意味着上下文菜单未正确更新的工作簿现在有 2 个复制按钮和 2 个粘贴按钮。

【问题讨论】:

    标签: c# excel vsto


    【解决方案1】:

    Office 2010 已弃用命令栏。您需要改用 Fluent UI 控件。

    更多信息请参见Customizing Context Menus in Office 2010

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 1970-01-01
      • 2023-03-10
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多