MS Word中自定义任务窗格将消失,当我编程方式打开文档 [英] MS Word Custom Task Pane disappears when I programatically open a document

查看:261
本文介绍了MS Word中自定义任务窗格将消失,当我编程方式打开文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的MS Word插件(主要是探索功能)。该插件增加了一个自定义任务窗格,以及组中的功能区。功能区控制包括一个复选框来控制自定义任务窗格的知名度,和一个按钮来打开一个文档。当我测试在MS Word外接程序任务窗格显示正确,和该复选框正常工作。问题是,当我按一下按钮,打开一个新的文档,任务窗格是隐藏的,并且该复选框不再控制任务窗格中的知名度。这是怎么了?我怎能自定义任务窗格显示?

I am trying to create a simple MS Word addin (mostly to explore functionality). The addin adds a Custom Task Pane, and group in the Ribbon. The Ribbon controls include a checkbox to control the visibility of the Custom Task Pane, and a button to open a document. When I test the addin in MS Word the task pane shows up correctly, and the checkbox works correctly. The problem is, as soon as I click the button and open a new document, the task pane is hidden, and the checkbox no longer controls the the task pane's visibility. What is going wrong? How can I keep the custom task pane showing?

下面是插件的一个简单的版本:

Here is a simple version of the addin:

public partial class ThisAddIn
{
    private MyUserControl _myUserControl;
    private CustomTaskPane _myCustomTastPane;
    private OpenFileDialog _dialog;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        _dialog = new OpenFileDialog { Filter = "Doc File (*.rtf)|*.rtf", RestoreDirectory = true };
        _myUserControl = new MyUserControl();
        _myCustomTastPane = this.CustomTaskPanes.Add(_myUserControl, "My Task Pane");
        _myCustomTastPane.Visible = true;

        Globals.Ribbons.MyRibbon.ShowPane.Click += ShowClicked;
        Globals.Ribbons.MyRibbon.LoadDoc.Click += LoadFile;
    }

    private void ShowClicked(object sender, RibbonControlEventArgs ribbonControlEventArgs)
    {
        _myCustomTastPane.Visible = Globals.Ribbons.MyRibbon.ShowPane.Checked;
    }

    void LoadFile(object sender, RibbonControlEventArgs e)
    {
        if (_dialog.ShowDialog() != DialogResult.OK) return;

        object missing = Missing.Value;
        object myFalse = false;
        object myTrue = true;
        object format = WdSaveFormat.wdFormatRTF;
        object fileToOpen = _dialog.FileName;

        Application.Documents.Open(ref fileToOpen, ref myFalse, ref myFalse, ref myFalse, ref missing, ref missing, ref missing, ref missing,
                                                                ref missing, ref missing, ref missing, ref myTrue, ref myFalse, ref missing, ref missing, ref missing);
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code
    //....
    #endregion
}

为了简单起见,我省略了功能区的定义,因为它实际上只是一个按钮和一个复选框。我也留下了的MyUserControl的定义,作为类的内容并不重要(在我的演示版本,我只是有一个简单的类标签)。

To keep things simple I've left out the definition of the ribbon, as it is really just a button and a checkbox. I'm also left out the definition of MyUserControl, as the content of the class isn't really important (in my demo version I just have a simple class with a label).

推荐答案

在微软的Word,自定义任务窗格的每个文档窗口(的请参阅MSDN参考)。如果你打开​​一个新的文档,任务窗格集合是不同的。如果你想保持一个持久的任务窗格中打开 - 你将不得不通过如下所述监控文件打开/关闭事件来管理它自己。

In MS Word, Custom Task Panes are per-document window (see MSDN Reference). If you open a new document, the Task Pane collection is different. If you want to keep a persistent task pane open - you would have to manage it yourself by monitoring document open/close events as stated below.

当你创建一个自定义任务窗格Word 2007或2007年InfoPath进行,在自定义任务窗格是可见的,只有一个单一的文件。在这些应用任务窗格与承载文件窗口相关联,但此窗口为每一个文档的不同实例。
  ...
    如果你想显示自定义任务窗格的多个文档,你可以当用户创建一个新的文档或打开现有文档中创建自定义任务窗格的新实例。例如,您可以创建处理程序<一href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.applicationevents4_event.newdocument.aspx"><$c$c>NewDocument或<一href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.applicationevents4_event.documentopen.aspx"><$c$c>DocumentOpen在Word 2007中的加载项活动,以创建自定义任务窗格的是与新的或打开的文档可见一个新的实例。

When you create a custom task pane for Word 2007 or InfoPath 2007, the custom task pane is visible only for a single document. Task panes in these applications are associated with the window that hosts documents, but there is a different instance of this window for every document.
...
If you want to display a custom task pane for multiple documents, you can create a new instance of the custom task pane when the user creates a new document or opens an existing document. For example, you can create handlers for the NewDocument or DocumentOpen events in a Word 2007 add-in to create a new instance of your custom task pane that is visible with the new or opened document.

这篇关于MS Word中自定义任务窗格将消失,当我编程方式打开文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆