Вопрос

I am trying to work out how to add a custom task pane (or indeed any kind of docking usercontrol) to a word document and every example I am finding is VSTO related.

I don't wish to use VSTO if I can help it.

Is it possible? Can anyone point me to the right place?

If this helps my goal is to present a form for additional metadata associated with certain documents, I can already detect on open or save if the document should or should not have this metadata, so it's a case of presenting a form so that it can be captured (to SQL or SharePoint).

     

Current code if you need it . . .

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) { }
}
Это было полезно?

Решение

As always I spend 3 hours googleing something, ask a question and then find the answer 30 mins later myself!

This was invaluable in getting it sorted http://www.shulerent.com/2011/01/23/adding-task-panes-in-a-office-add-in-when-using-idtextensibility2/

Need to inherit ICustomTaskPaneConsumer from Microsoft.Office.Core and implement CTPFactoryAvailable

I tweaked the example slightly to produce this.

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) { }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top