TaskPanes and TaskPaneInstances properties. Why are protected? Why two Collections?

Jul 31, 2013 at 12:33 PM
Edited Aug 1, 2013 at 10:15 AM
Sorry in advance. I'm just starting with NetOffice.

I'm writing a Word COM Addin. Given that a custom task pane is associated with a document frame window (word.document in Word), I need to manage many task pane instances. See Managing Custom Task Panes in Multiple Application Windows
Word, InfoPath, and PowerPoint display each document in a different document frame window. When you create a custom task pane for these applications, the custom task pane is associated only with a specific document. If the user opens a different document, the custom task pane is hidden until the earlier document is visible again.
A task panel for a given Word.Document will show custom content for this document. When I click on a ribbon command, I need to know if I’ve already created a task panel for this document or not, and know if it's visible.

Comparing standard VSTO with NetOffice, I realize that VSTO defines "CustomTaskPanes" as Friend (internal in C#).

But NetOffice has two collections: TaskPanes and TaskPaneInstances. Both are "Protected".

Why protected? Why not "Friend"?

Why two collections?

Which i must use for my purpose?

Why the "Add" method does not return the "TaskPaneInfo" created?. With a Function that return the newly created taskpaneinfo, it would be easier to reference the new object.

Instead of:
    TaskPanes.Add(GetType(PedirFechaTaskPaneControl), "Task Pane")
    TaskPanes(0).DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight
    TaskPanes(0).DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal
    TaskPanes(0).Width = 250
    TaskPanes(0).Visible = False
    TaskPanes(0).Arguments = New Object() {Me}
, we could write:
    Dim myTaskPaneInfo = TaskPanes.Add(GetType(PedirFechaTaskPaneControl), "Task Pane")
    myTaskPaneInfo.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight
    myTaskPaneInfo.DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal
    myTaskPaneInfo.Width = 250
    myTaskPaneInfo.Visible = False
    myTaskPaneInfo.Arguments = New Object() {Me}
As you can see, I'm not sure how to manage multiple task pane instances. Is there a known way to do it?
Coordinator
Aug 1, 2013 at 8:41 AM
hmmm not sure, i'm not an expert in Word. i have to clearify the situation. when its the same in an interop or VSTO solution, yes this is a Word "feature" i do some some tests and give you response asap.

*Sebastian
Coordinator
Aug 1, 2013 at 8:42 AM
*damn it okay now i see your edit. no, its okay, need to delete this, may helpful for other users.

*Sebastian
Aug 1, 2013 at 10:14 AM
Thank you for your reply and thank you for this software.

I've edited again to show a more concrete question. I suggest to adapt word examples to this behaviour. If you want, I can do it.

But, i don't understand why two collections and why protected. Unless we write all code in Addin class, we'll need a way to query task pane collections to locate the correct if exists.

Thank you again!
Aug 1, 2013 at 1:05 PM
Edited Aug 1, 2013 at 1:07 PM
I can't get it work using existing protected properties like "TaskPanes" and "TaskPaneInstances".

I've funcional code using my own list of custom task panes. It works. But I'm not convinced. If the framework has something to store and manage existing task panes, common sense suggest to use it.

NOTE: It's not complete. I don't manage Word events to destroy objects when documents are closed, etc, etc, ...
Public Class Addin
    Inherits COMAddin

    '...
#Region "Custom properties and functions to expose protected members"
    Friend ReadOnly Property WordApplication() As Word.Application
        Get
            Return MyBase.Application
        End Get
    End Property

    Friend ReadOnly Property TaskPaneFactoryObject() As NetOffice.OfficeApi.ICTPFactory
        Get
            Return MyBase.TaskPaneFactory
        End Get
    End Property
#End Region

    Public Property MyCustomTaskPaneList As New List(Of NetOffice.OfficeApi._CustomTaskPane) 
    '...
End Class
In a separate class, I pass an AddIn instance in constructor and store it like "_addin"
    Public Sub ShowTaskPane() '
        'If no active document nothing to do
        Dim activeDoc As NetOffice.WordApi.Document = _addin.WordApplication.ActiveDocument
        If activeDoc Is Nothing Then
            Return
        End If

        'Traditional way. Using Linq would be:
        '   foundTaskPane = _addin.MyCustomTaskPaneList.FirstOrDefault(Function(f) f.Window IsNot Nothing AndAlso CType(f.Window, NetOffice.WordApi.Window).Hwnd = activeDoc.ActiveWindow.Hwnd)
        Dim foundTaskPane As NetOffice.OfficeApi._CustomTaskPane = Nothing
        For Each tp In _addin.MyCustomTaskPaneList
            Dim w As NetOffice.WordApi.Window = tp.Window
            If w IsNot Nothing AndAlso w.Hwnd = activeDoc.ActiveWindow.Hwnd Then
                foundTaskPane = tp
                Exit For
            End If
        Next
        If foundTaskPane Is Nothing Then
            'Current Word document hasn't associated task pane
            Dim newTaskPane As NetOffice.OfficeApi._CustomTaskPane = _addin.TaskPaneFactoryObject.CreateCTP(GetType(PedirFechaTaskPaneControl).FullName,
                                                                                                   activeDoc.Name,
                                                                                                   activeDoc.ActiveWindow)
            newTaskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight
            newTaskPane.DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal
            newTaskPane.Width = 250
            newTaskPane.Visible = True
            _addin.MyCustomTaskPaneList.Add(newTaskPane)
        Else
            'Do something ...
            foundTaskPane.Visible = Not foundTaskPane.Visible
        End If
    End Sub
Again, thank you for this wonderful software.
Coordinator
Aug 2, 2013 at 4:33 PM
i have no idea what vsto doesn and its not important for me.the TaskPanes property stores the definition and the TaskPaneInstances property the created instances from.

you find a word sample addin(regarding the tools namesapce) with a custom task pane in the download section. i hope this helps you.

Sebastian