C# 向Word添加自定义任务窗格(不使用VSTO)

C# 向Word添加自定义任务窗格(不使用VSTO),c#,ms-word,add-in,C#,Ms Word,Add In,我正在尝试解决如何将自定义任务窗格(或者任何类型的停靠用户控件)添加到word文档中,我发现的每个示例都与VSTO相关 如果我能帮上忙,我不想使用VSTO 可能吗?谁能给我指对地方吗 如果这有助于我的目标是为与某些文档关联的其他元数据呈现表单,那么我已经可以在打开或保存时检测文档是否应该包含此元数据,因此这是一种呈现表单的情况,以便可以捕获(到SQL或SharePoint) 当前代码,如果您需要它 public class WordApplication : Extensibility.ID

我正在尝试解决如何将自定义任务窗格(或者任何类型的停靠用户控件)添加到word文档中,我发现的每个示例都与VSTO相关

如果我能帮上忙,我不想使用VSTO

可能吗?谁能给我指对地方吗

如果这有助于我的目标是为与某些文档关联的其他元数据呈现表单,那么我已经可以在打开或保存时检测文档是否应该包含此元数据,因此这是一种呈现表单的情况,以便可以捕获(到SQL或SharePoint)

当前代码,如果您需要它

public class WordApplication : Extensibility.IDTExtensibility2
{
    private Microsoft.Office.Interop.Word.Application WordApp;

    public void OnConnection(object Application, Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
    {
        WordApp = Application as Microsoft.Office.Interop.Word.Application;
        WordApp.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(WordApp_DocumentOpen);
    }

    void WordApp_DocumentOpen(Word.Document Doc)
    {
        if (isPaneRequired(Doc))
        {
            ShowSomeSortOfPane();
        }
    }

    private bool isPaneRequired(Word.Document Doc) { return true; } //Lots of code not needed for example.

    private void ShowSomeSortOfPane()
    {
        //What goes here?
    }

    public void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref Array custom) { }
    public void OnStartupComplete(ref Array custom) { }
    public void OnAddInsUpdate(ref Array custom) { }
    public void OnBeginShutdown(ref Array custom) { }
}

和往常一样,我花了3个小时在谷歌上搜索一些东西,问一个问题,然后自己在30分钟后找到答案

这对于分类是非常宝贵的

需要从Microsoft.Office.Core继承ICustomTaskPaneConsumer并实现CTPFactoryAvailable

我稍微调整了这个示例以产生这个结果

public class WordApplication : Extensibility.IDTExtensibility2
{
    private Microsoft.Office.Interop.Word.Application WordApp;
    private ICTPFactory myCtpFactory;
    private CustomTaskPane myPane;
    private tskPane myControl; //My UserControl

    public void OnConnection(object Application, Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
    {
        WordApp = Application as Microsoft.Office.Interop.Word.Application;
        WordApp.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(WordApp_DocumentOpen);
    }

    public void CTPFactoryAvailable(ICTPFactory CTPFactoryInst)
    {
        myCtpFactory = CTPFactoryInst;
    }

    void WordApp_DocumentOpen(Word.Document Doc)
    {
        if (isPaneRequired(Doc)) ShowSomeSortOfPane();
    }

    private bool isPaneRequired(Word.Document Doc) { return true; } //Lots of code not needed for example.

    private void ShowSomeSortOfPane()
    {
        myPane = myCtpFactory.CreateCTP("NameSpace.UserControlClassName", "My Task Pane", Type.Missing);
        myPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
        myControl = (tskPane)myPane.ContentControl;

        myControl.CustomProperty = CustomValue;
        myPane.Visible = true;

    }

    public void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref Array custom) { }
    public void OnStartupComplete(ref Array custom) { }
    public void OnAddInsUpdate(ref Array custom) { }
    public void OnBeginShutdown(ref Array custom) { }
}

tskPane是什么东西?我使用了TaskPane,但在其上找不到“CustomProperty”。它是基于UserControl的自定义类。我认为,只要右键单击您的解决方案并添加一个新的UserControl。