从Application.CommandBars [“Cell”]删除CommandBarButtons不会从ThisAddIn_Shutdown事件中的所有打开的工作簿中删除它们

时间:2014-12-08 22:51:57

标签: c# excel vsto

我有一个带.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,我知道有一个与右键单击上下文菜单有关的错误(这就是为什么我使用foreach作为CommandBarControls而不是使用全局变量)。您发现工作的任何工作流程都会非常感谢!

TO CLARIFY:一切正常,唯一的问题是如果Add In关闭,CommandBarButtons不会从非活动工作簿的右键单击上下文菜单中删除。如果在同一会话期间重新打开“添加”,则会再次为所有工作簿提供“复制”和“粘贴”按钮,这意味着其上下文菜单未正确更新的工作簿现在具有2个“复制”按钮和2个“粘贴”按钮。

1 个答案:

答案 0 :(得分:1)

Office 2010不推荐使用命令栏。您需要使用Fluent UI控件。

有关详细信息,请参阅Customizing Context Menus in Office 2010