none
How to close all custom task panes with a button? RRS feed

  • Question

  • Hi,

    I'm using VS2015 and Excel 2016 to develop my addin.

    I've already created a task pane called MyTaskPane.

     Private Sub MyTaskPane_Click(sender As Object, e As RibbonControlEventArgs) Handles MyTaskPane.Click
    
    Dim ctp As Microsoft.Office.Tools.CustomTaskPane =
     Globals.ThisAddIn.CustomTaskPanes.Add(New MyTaskPane, "My Task Pane")
    
    End Sub

    And now I'd like to add an another button to close all the task panes I opened. Is there any syntax I could use?  

    Thanks in advance. Hope you have a nice day!


    • Edited by AuditorQQ Wednesday, March 8, 2017 3:17 PM Typo
    Wednesday, March 8, 2017 3:17 PM

Answers

  • Here's some code that might help.

        Private Sub Close_Button_Click(sender As Object, e As EventArgs) Handles Close_Button.Click
            Me.Dispose()
            Globals.ThisAddIn.RemoveScriptElementsTaskPane()
            Globals.ThisAddIn.RemoveOrphanedTaskPanes()
        End Sub
    
        Private Sub ScriptElements_Close(sender As Object, e As EventArgs) Handles MyBase.Disposed
            Globals.ThisAddIn.RemoveScriptElementsTaskPane()
            Globals.ThisAddIn.RemoveOrphanedTaskPanes()
        End Sub
    
        Public Sub RemoveScriptElementsTaskPane()
            On Error Resume Next
            Dim ctp As Microsoft.Office.Tools.CustomTaskPane
            For i As Integer = Me.CustomTaskPanes.Count To 1 Step -1
                ctp = Me.CustomTaskPanes.Item(i - 1)
                If ctp.Title = My.Resources.thisaddin_ScriptElements Then
                    Me.CustomTaskPanes.Remove(ctp)
                End If
            Next
        End Sub
    
    
        Public Sub RemoveOrphanedTaskPanes()
            Dim ctp As Microsoft.Office.Tools.CustomTaskPane
            If Me.CustomTaskPanes.Count = 0 Then Exit Sub
            For i As Integer = Me.CustomTaskPanes.Count To 1 Step -1
                ctp = Me.CustomTaskPanes.Item(i - 1)
                If ctp.Window Is Nothing Then
                    Me.CustomTaskPanes.Remove(ctp)
                End If
            Next
        End Sub

    The two private subs are part of the code in the task pane. The two public subs are in the ThisAddin routine.

    I've made it a habit to alway run the RemoveOrphaned... routine just to be safe.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Wednesday, March 8, 2017 8:59 PM