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:
- Start Word - new document opened, all works fine
- Open existing document (closes empty document)
- Click on toggle button, pane doesn't appear
- Create new document or open ANOTHER existing document, pane appears on that document
- 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