Imports EnvDTE Imports System.Diagnostics Public Module CollapseSolutionExplorer Sub QuickCollapseAndClose() CloseAllWindows(False) DoCollapse(True) End Sub Sub QuickCollapseAndCloseKeepActive() CloseAllWindows(True) DoCollapse(True) End Sub Sub QuickCollapse() DoCollapse(True) End Sub Sub CompleteCollapse() DoCollapse(False) End Sub Private Sub DoCollapse(ByVal quick As Boolean) ' Get the the Solution Explorer tree Dim UIHSolutionExplorer As UIHierarchy UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object() ' Check if there is an open solution If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then Return End If ' Get the top node (the name of the solution) Dim UIHSolutionRootNode As UIHierarchyItem UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1) ' Hide the Solution Explorer to prevent multiple screen paints DTE.StatusBar.Text = "Solution Explorer hidden... it will return..." Dim activeWindow As EnvDTE.Window activeWindow = UIHSolutionExplorer.DTE.ActiveWindow DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Visible = False ' Select the solution (top) node so that the open document isn't automatically expanded UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect) ' Collapse each project node Dim UIHItem As UIHierarchyItem For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems ' the first-level is always collapsed, regardless of quick or not ' this is a nice feature that makes the quick collapse more useful Collapse(UIHItem, quick) Next ' Bring back the Solution Explorer DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Visible = True ' Clear the status bar DTE.StatusBar.Clear() DTE.StatusBar.Progress(False) End Sub Private Sub Collapse(ByVal item As UIHierarchyItem, ByVal quick As Boolean) 'recursive function For Each eitem As UIHierarchyItem In item.UIHierarchyItems If ((quick = True) And (eitem.UIHierarchyItems.Expanded = True)) Or (quick = False) Then If eitem.UIHierarchyItems.Count > 0 Then DTE.StatusBar.Text = "Solution Explorer hidden... it will return... Collapsing " & item.Name Collapse(eitem, quick) ' recursion here End If End If Next item.UIHierarchyItems.Expanded = False End Sub Private Sub CloseAllWindows(ByVal KeepCurrentOpen As Boolean) On Error Resume Next Dim i As Integer Dim sCurrWin As String = DTE.ActiveDocument.Name With DTE For i = .Documents.Count To 1 Step -1 If .Documents.Item(i).Name <> sCurrWin Or KeepCurrentOpen = False Then .Documents.Item(i).Close(vsSaveChanges.vsSaveChangesYes) End If Next End With End Sub End Module