title: Word 2007 Add-in Task Pane doesn't work in one specific case
body: <p>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:</p>
  
  <ol>
  <li>Start Word - new document opened, all works fine</li>
  <li>Open existing document (closes empty document)</li>
  <li>Click on toggle button, pane doesn't appear</li>
  <li>Create new document or open ANOTHER existing document, pane appears on that document</li>
  <li>Pane now works as expected on all documents, including the problem one from 2/3.</li>
  </ol>
  
  <p>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.</p>
  
  <p>Here's my code from ThisAddIn class:</p>
  
  <p>Note that the PaneControl is a totally empty User Control, behaviour doesn't change when I add stuff to it.</p>
  
  <pre><code>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 &gt; 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
  
      /// &lt;summary&gt;
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// &lt;/summary&gt;
      private void InternalStartup()
      {
          this.Startup += new System.EventHandler(ThisAddIn_Startup);
          this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
      }
  
      #endregion
  </code></pre>
  
  <p>The code attached to the ribbon button is:</p>
  
  <pre><code>        Globals.ThisAddIn.ToggleTaskPane(Globals.ThisAddIn.Application.ActiveWindow);
  </code></pre>
  
  <p>Any ideas why this might be happening?</p>
  
  <p>Thanks</p>
  
  <p>ROSCO</p>