2

我正在使用 VSTO 和 c# 创建一个插件,

我已经为任务窗格创建了一个用户控件,其中我有以下控件:1)一个文本框 - 用于 Word 文档文件夹,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 对象。”

帮帮我。

4

0 回答 0