VSTO- word taskpane在具有价值问题的多个文档中共享

时间:2012-09-28 10:49:41

标签: c# vsto office-interop

我使用带有c#,

的VSTO创建一个插件

我为任务窗格创建了一个用户控件,我有以下控件: 1)文本框换文字文件夹, 2)一个按钮 - 用于在文本框中浏览抛出指定文件夹, 3)树视图控件,用于显示文本框中指定路径上的所有文件和文件夹,

这些工作正常。当我从树视图中选择一个文档时,它会在没有任务窗格的情况下打开。

我已经为文档选择编写了以下代码。

private void treeFiles_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        string fullPath = e.Node.Name;
        string checkExt = Path.GetExtension(fullPath);
        if (checkExt.Contains("doc"))
        {
            //System.Diagnostics.Process.Start(fullPath);
            object fileName = fullPath;
            object readOnly = false;
            object isVisible = true;
            // Here is the way to handle parameters you don't care about in .NET
            object missing = System.Reflection.Missing.Value;

            // Make word visible, so you can see what's happening         
            Globals.MainPage.Application.Visible = true;

            Word.Document aDoc = Globals.MainPage.Application.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);

            // Activate the document so it shows up in front
            aDoc.Activate();
        }
    }

在主文件中我编写了以下代码:

    HomeControl objHomeControl;

    public static Microsoft.Office.Tools.CustomTaskPane taskPaneValue;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        objHomeControl = new HomeControl();
        taskPaneValue = this.CustomTaskPanes.Add(objHomeControl, "File task pane");
        taskPaneValue.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft;
        taskPaneValue.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
        taskPaneValue.Visible = true;
        this.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);

    }

    void Application_DocumentOpen(Word.Document Doc)
    {
        HomeControl objHomeControl1 = new HomeControl();
        taskPaneValue = this.CustomTaskPanes.Add(objHomeControl1, "File task pane",Doc.ActiveWindow);
        taskPaneValue.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft;
        taskPaneValue.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
        taskPaneValue.Visible = true;
    }

    void taskPaneValue_VisibleChanged(object sender, EventArgs e)
    {
        Globals.Ribbons.MyRibbon.tgleButton.Checked = taskPaneValue.Visible;

    }

    public Microsoft.Office.Tools.CustomTaskPane TaskPane
    {
        get
        {
            return taskPaneValue;
        }
    }

当我双击节点时,文档打开但是添加(UserControl)重新初始化。所有值都重置,从功能区添加开/关不起作用。如何在所有文档中制作UserControl共享的单个副本。

我也试过这个

    void Application_DocumentOpen(Word.Document Doc)
    {
        taskPaneValue = this.CustomTaskPanes.Add(objHomeControl, "File task pane",Doc.ActiveWindow);
        taskPaneValue.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft;
        taskPaneValue.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
        taskPaneValue.Visible = true;
    }

它打开新文档中的任务窗格。但不允许我点击任何地方。如果我点击父文档,我会收到以下异常消息:

“无法使用已与其基础RCW分离的COM对象。”

帮帮我。

0 个答案:

没有答案