将功能区按钮与多个窗口VSTO中的任务窗格同步

时间:2018-05-19 10:11:56

标签: c# vsto word-addins word-automation

我正在开发一个Word插件,它有一个用于显示和隐藏任务窗格的切换按钮和一个用于更改任务窗格中标签的普通按钮。

当我一次打开一个文档实例时它工作正常。如果我打开多个文档,切换按钮工作正常:即,当我点击它时,它将显示/隐藏任务窗格。但是当我点击普通按钮时,它将改变另一个文档窗口中的标签。

This is my ribbon

Addin类代码:

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();
}
}

我已浏览过这些链接: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

但我无法解决问题。

1 个答案:

答案 0 :(得分:0)

我猜您尝试使用以下对象引用修改标签:

 public UserControl1 objsidepane;

如果是这样,每次更改活动文档时都需要更新引用:

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