Collapsing Visual Studio Solution Explorer to Project Definitions

Share on:

Here’s a little macro I’ve just knocked up that will collapse a Visual Studio Solution to just its project definitions - put here so you can use it too and I have access to it everywhere I work!

 1
 2Sub CollapseToProjects() 
 3    Dim items As UIHierarchyItems 
 4    Dim i As Integer 
 5
 6    ' Get the root Solution node 
 7    items = DTE.ToolWindows.SolutionExplorer.UIHierarchyItems() 
 8    If items.Count > 0 Then 
 9        ' Get a pointer to all the nodes under the solution node 
10        items = items.Item(1).UIHierarchyItems() 
11
12        For i = 1 To items.Count 
13            ' Recursively collapse any expanded items 
14            CollapseItems(items.Item(i).UIHierarchyItems) 
15        Next 
16    End If 
17End Sub 
18
19Private Sub CollapseItems(ByVal items As UIHierarchyItems) 
20    Dim i As Integer 
21
22    ' Only mess with the item if it's already expanded 
23    If items.Expanded Then 
24
25        ' Recurse into the item to collapse any children 
26        For i = 1 To items.Count 
27            CollapseItems(items.Item(i).UIHierarchyItems) 
28        Next 
29
30        ' Collapse the items 
31        items.Expanded = False 
32    End If 
33End Sub