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).