0
votes

I am developing a Word addin which has a toggle button to show and hide the taskpane and a normal button to change a label in the taskpane.

It is working fine when I open one instance of a document at a time. If I open multiple documents the toggle button is working fine: ie, when I click on it it will show/hide the taskpane. But when I click on the normal button it will change the label in the other document window.

This is my ribbon

Addin class code:

public partial class ThisAddIn
{
public UserControl1 objsidepane;
public Microsoft.Office.Tools.CustomTaskPane myCustomtask;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        AddCalendarTaskPane(this.Application.ActiveDocument);     
        Word.ApplicationEvents2_Event wdEvents2 = (Word.ApplicationEvents2_Event)this.Application;
        wdEvents2.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(Application_NewDocument);
        this.Application.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(Application_DocumentChange);
        this.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
    }

private void Application_DocumentOpen(Word.Document Doc)
    {
        AddTaskPane(Doc);
    }

private void Application_DocumentChange()
    {
    this.myCustomtask = GetTaskPaneForWindow(this.Application.ActiveDocument, this.Application.ActiveWindow);
    }

private Microsoft.Office.Tools.CustomTaskPane GetTaskPaneForWindow(Word.Document doc, Word.Window wn)
    {
        foreach (Microsoft.Office.Tools.CustomTaskPane CTP in Globals.ThisAddIn.CustomTaskPanes)
        {
            if (object.ReferenceEquals(CTP.Window, Globals.ThisAddIn.Application.ActiveWindow) && CTP.Title == "sidepane")
            {

                return CTP;
            }
        }
    }
public void AddTaskPane(Word.Document doc)
    {
        objsidepane = new UserControl1();
        myCustomtask = this.CustomTaskPanes.Add(objsidepane, "sidepane",this.Application.ActiveDocument.ActiveWindow);
        myCustomtask.VisibleChanged += new EventHandler(Usercontrol_VisibleChanged);
        myCustomtask.Width = 350;
    }
private void Application_NewDocument(Word.Document Doc)
    {

        AddTaskPane(Doc);
    }
private void Usercontrol_VisibleChanged(object sender, EventArgs e)
    {
        Globals.Ribbons.Ribbon1.toggleButton1.Checked = myCustomtask.Visible;
    }

public Microsoft.Office.Tools.CustomTaskPane TaskPane
    {
        get
        {
            foreach (Microsoft.Office.Tools.CustomTaskPane CTP in Globals.ThisAddIn.CustomTaskPanes)
            {
                if (object.ReferenceEquals(CTP.Window, Globals.ThisAddIn.Application.ActiveWindow) && CTP.Title == "sidepane")
                {

                    return CTP;
                }
            }
            return null;
        }
    }

Ribbon.cs

public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{

}

private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
{
    Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
}

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    render();
}
}

I have gone through these links: https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb264456(v=office.12)

Creating and managing custom task panes for multiple documents in a VSTO Word addin

but I could not solve the issue.

1
What have you tried? The button1_Click method in your code is empty, except for a call to render(), for which you don't show the code.Cindy Meister

1 Answers

0
votes

I guess you try to modify the label using the following object reference:

 public UserControl1 objsidepane;

If so, you need to update the reference each time the active document is changed:

private void Application_DocumentChange()
{
      this.myCustomtask = GetTaskPaneForWindow(this.Application.ActiveDocument, this.Application.ActiveWindow);
      this.objsidepane = (UserControl1)this.myCustomtask.Control;
}