I use the below code to manage different custom task panes in PowerPoint VSTO across presentations. This works fine, e.g. when a user opens a new presentation a new task pane is created and it does not affect any other open presentation task panes.
Now I encountered the following situation. A user has opened a presentation and now opens an additional window in PowerPoint for this presentation (click "View", "New window"). Now what happens is that a new custom task pane (because this window's HWND is different) is created but instead I need this task pane to be the same as in the other presentation window.
Question: how can I "share" a task pane between all windows of the same presentation?
Dim CreatedPanes As New Dictionary(Of String, CustomTaskPane)
Public Function GetTaskPane(taskPaneId As String, taskPaneTitle As String) As Microsoft.Office.Tools.CustomTaskPane
Dim key As String = $"{taskPaneId}({Globals.ThisAddIn.Application.HWND})"
If Not CreatedPanes.ContainsKey(key) Then
Dim pane = Globals.ThisAddIn.CustomTaskPanes.Add(New myTaskPaneControl(), taskPaneTitle)
CreatedPanes(key) = pane
End If
Return CreatedPanes(key)
End Function
I think the same logic will apply to Excel as well, hence I am sldo adding this tag to the question.
is created but instead I need this task pane to be the same as in the other presentation window.
It seems you just need to implement a singleton pattern for custom task panes. I.e. share the same information for all other instances of the task pane to keep the data up to date.
PowerPoint displays 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.
If you want to display a custom task pane with multiple documents, create a new instance of the custom task pane when the user creates a new document or opens an existing document. To do this, handle events that are raised when a document is created or opened, and then create the task pane in the event handlers. You can also handle document events to hide or display task panes depending on which document is visible.
To associate the task pane with a specific document window, use the Add
method to create the task pane, and pass a DocumentWindow
(for PowerPoint) to the window parameter.
To monitor the state of document windows in PowerPoint, you can handle the following events:
Microsoft.Office.Interop.PowerPoint.EApplication_Event.AfterNewPresentation
Microsoft.Office.Interop.PowerPoint.EApplication_Event.AfterPresentationOpen
Microsoft.Office.Interop.PowerPoint.EApplication_Event.NewPresentation
Microsoft.Office.Interop.PowerPoint.EApplication_Event.PresentationOpen
Microsoft.Office.Interop.PowerPoint.EApplication_Event.WindowActivate
Microsoft.Office.Interop.PowerPoint.EApplication_Event.WindowDeactivate