我刚刚开始使用Word VSTO加载项 . 我想在功能区中添加一个组,其中包含一个用于切换自定义任务窗格的按钮 . 我希望每个文档都有自己独立的任务窗格 . 我有这个主要工作,但有一种情况不起作用:

  • 启动Word - 打开新文档,一切正常

  • 打开现有文档(关闭空文档)

  • 单击切换按钮,窗格不会出现

  • 创建新文档或打开另一个现有文档,窗格显示在该文档上

  • 窗格现在按预期在所有文档上工作,包括2/3中的问题 .

请注意,如果您在新文档(1)中键入内容,一切都按预期工作,所以这似乎与现有文档加载在初始空文档的顶部有关,但我无法弄清楚会发生什么上 .

这是我在ThisAddIn类中的代码:

请注意,PaneControl是一个完全空的用户控件,当我向其添加内容时,行为不会改变 .

public partial class ThisAddIn
{
    private CustomTaskPane CurrentTaskPane(Object window)
    {
        foreach (CustomTaskPane ctp in CustomTaskPanes)
        {
            if (ctp.Window.Equals(window))
            {
                return ctp;
            }
        }
        return null;
    }
    public bool ToggleTaskPane(Object window)
    {
        CustomTaskPane ctp = CurrentTaskPane(window);
        if (ctp != null)
        {
            ctp.Visible = !ctp.Visible;
            return ctp.Visible;
        }
        else
        {
            return false;
        }
    }
    private void RemoveOrphanedTaskPanes()
    {
        for (int i = CustomTaskPanes.Count; i > 0; i--)
        {
            var ctp = CustomTaskPanes[i - 1];
            if (ctp.Window == null)
            {
                CustomTaskPanes.Remove(ctp);
            }
        }
    }
    private void CreateTaskPane(Object window)
    {
        try
        {
            RemoveOrphanedTaskPanes();
            // Add the new one
            PaneControl ucPaneControl = new PaneControl();
            CustomTaskPane ctp = CustomTaskPanes.Add(ucPaneControl, "Test Pane", window);
            ctp.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
            ctp.Width = 300;
        }
        catch
        {
            MessageBox.Show("Unable to create pane");
        }
    }

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        try
        {
            Word.ApplicationEvents4_Event app = (Word.ApplicationEvents4_Event)Application; // Disambiguate
            app.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
            app.NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
            app.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(Application_DocumentChange);
            CreateTaskPane(Application.ActiveWindow);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        RemoveOrphanedTaskPanes();
    }
    void Application_DocumentChange()
    {
        RemoveOrphanedTaskPanes();
    }
    void Application_DocumentOpen(Word.Document Doc)
    {
        // Creeate pane for existing document
        CreateTaskPane(Doc.ActiveWindow);
    }
    void Application_NewDocument(Word.Document Doc)
    {
        // Creeate pane for new blank document
        CreateTaskPane(Doc.ActiveWindow);
    }

    #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

附加到功能区按钮的代码是:

Globals.ThisAddIn.ToggleTaskPane(Globals.ThisAddIn.Application.ActiveWindow);

任何想法为什么会这样?

谢谢

ROSCO