Word 2007 Add-in Task Pane doesn't work in one specific case
Asked Answered
U

0

2

I am just starting out with a Word VSTO Add-in. I want to add a group to the ribbon, which has a button to toggle a custom task pane. I want each document to have it's own independent task pane. I have this mostly working, but there is one situation that doesn't work:

  1. Start Word - new document opened, all works fine
  2. Open existing document (closes empty document)
  3. Click on toggle button, pane doesn't appear
  4. Create new document or open ANOTHER existing document, pane appears on that document
  5. Pane now works as expected on all documents, including the problem one from 2/3.

Note that if you type something into the new document (1), everything works as expected, so this seems to be something to do with the existing document loading over the top of the initial empty one, but I can't work out what's going on.

Here's my code from ThisAddIn class:

Note that the PaneControl is a totally empty User Control, behaviour doesn't change when I add stuff to it.

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

The code attached to the ribbon button is:

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

Any ideas why this might be happening?

Thanks

ROSCO

Unbuckle answered 12/8, 2013 at 1:31 Comment(2)
Well, this issue has solved itself, but I'm not sure exactly what change fixed it. What happened was, I changed the project to target .NET 4.0 (it was originally targeting 3.5), then went back to 3.5. I did make some other small and (I think) unrelated changes at the same time, but that's what seems to have fixed it. The config and all references appear to have gone back to the original, so I really don't know what caused the problem or what exactly fixed it.Unbuckle
I add reference to this q&a as it's quite similar problem and the answer there is important one.Topazolite

© 2022 - 2024 — McMap. All rights reserved.