5
votes

I am trying to create a simple MS Word addin (mostly to explore functionality). The addin adds a Custom Task Pane, and group in the Ribbon. The Ribbon controls include a checkbox to control the visibility of the Custom Task Pane, and a button to open a document. When I test the addin in MS Word the task pane shows up correctly, and the checkbox works correctly. The problem is, as soon as I click the button and open a new document, the task pane is hidden, and the checkbox no longer controls the the task pane's visibility. What is going wrong? How can I keep the custom task pane showing?

Here is a simple version of the addin:

public partial class ThisAddIn
{
    private MyUserControl _myUserControl;
    private CustomTaskPane _myCustomTastPane;
    private OpenFileDialog _dialog;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        _dialog = new OpenFileDialog { Filter = "Doc File (*.rtf)|*.rtf", RestoreDirectory = true };
        _myUserControl = new MyUserControl();
        _myCustomTastPane = this.CustomTaskPanes.Add(_myUserControl, "My Task Pane");
        _myCustomTastPane.Visible = true;

        Globals.Ribbons.MyRibbon.ShowPane.Click += ShowClicked;
        Globals.Ribbons.MyRibbon.LoadDoc.Click += LoadFile;
    }

    private void ShowClicked(object sender, RibbonControlEventArgs ribbonControlEventArgs)
    {
        _myCustomTastPane.Visible = Globals.Ribbons.MyRibbon.ShowPane.Checked;
    }

    void LoadFile(object sender, RibbonControlEventArgs e)
    {
        if (_dialog.ShowDialog() != DialogResult.OK) return;

        object missing = Missing.Value;
        object myFalse = false;
        object myTrue = true;
        object format = WdSaveFormat.wdFormatRTF;
        object fileToOpen = _dialog.FileName;

        Application.Documents.Open(ref fileToOpen, ref myFalse, ref myFalse, ref myFalse, ref missing, ref missing, ref missing, ref missing,
                                                                ref missing, ref missing, ref missing, ref myTrue, ref myFalse, ref missing, ref missing, ref missing);
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code
    //....
    #endregion
}

To keep things simple I've left out the definition of the ribbon, as it is really just a button and a checkbox. I'm also left out the definition of MyUserControl, as the content of the class isn't really important (in my demo version I just have a simple class with a label).

1

1 Answers

6
votes

In MS Word, Custom Task Panes are per-document window (see MSDN Reference). If you open a new document, the Task Pane collection is different. If you want to keep a persistent task pane open - you would have to manage it yourself by monitoring document open/close events as stated below.

From MSDN...

When you create a custom task pane for Word 2007 or InfoPath 2007, the custom task pane is visible only for a single document. Task panes in these applications are associated with the window that hosts documents, but there is a different instance of this window for every document.
...
If you want to display a custom task pane for multiple documents, you can create a new instance of the custom task pane when the user creates a new document or opens an existing document. For example, you can create handlers for the NewDocument or DocumentOpen events in a Word 2007 add-in to create a new instance of your custom task pane that is visible with the new or opened document.