Search
Close this search box.

Visual Studio 2005 Collapse All Macro

Visual Studio has this annoying habit of expanding the solution folder tree, especially when synchronizing with Visual SourceSafe.  Even without that problem I sometimes find myself with many expanded tree nodes in the Solution Explorer and I want a quick way to collapse them all.

So I searched for a macro based solution to the problem and came up with the VS.Net Macro: Collapse All from Captain LoadTest.  This macro was not designed to collapse a non-expanded folder’s sub items.  So I made a few quick changes to get the macro to walk the entire tree and collapse every item that was expanded.

But is was not working.  Some items just were not collapsing, so I ended up with more items expanded than when I started because Visual Studio will expand the items while you walk the tree.  It turns out I ran into a bug in Visual Studio 2005 that should have been fixed in SP1 and that was marked as fixed by Microsoft, but still is not fixed.  Carlos J. Quintero mentioned a work-around in a Microsoft forum that seems to do the job.

The code to collapse everything does take longer to run (10 seconds across a very large solution, compared to 1 second with the original script) but it does work now.

So now that everything is working here is the code:

Sub CollapseAll()

        ' Get the the Solution Explorer tree Dim solutionExplorer As
    UIHierarchy solutionExplorer =
        DTE.Windows.Item(Constants.vsext_wk_SProjectWindow)
            .Object()

        ' Check if there is any open solution If(
                solutionExplorer.UIHierarchyItems.Count = 0) Then Return End If

        ' Get the top node (the name of the solution) Dim rootNode As
        UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1) rootNode.DTE
                              .SuppressUI =
            True

        ' Collapse each project node Collapse(rootNode, solutionExplorer)

        ' Select the solution node, or else when you click 
        ' on the solution window
        ' scrollbar, it will synchronize the open document 
        ' with the tree and pop
        ' out the corresponding node which is probably not what you want.

            rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect) rootNode
                .DTE.SuppressUI =
                False

                End Sub

                Private Sub Collapse(ByVal item As UIHierarchyItem,
                                     ByRef solutionExplorer As UIHierarchy)

                    For Each innerItem As UIHierarchyItem In item
                        .UIHierarchyItems If innerItem.UIHierarchyItems.Count
                > 0 Then

                ' Re-cursive call Collapse(innerItem, solutionExplorer)

                ' Collapse If innerItem.UIHierarchyItems
                      .Expanded Then innerItem.UIHierarchyItems.Expanded =
                    False If innerItem.UIHierarchyItems.Expanded =
                        True Then
                        ' Bug in VS 2005 innerItem
                            .Select(vsUISelectionType.vsUISelectionTypeSelect)
                                solutionExplorer.DoDefaultAction() End If End If

                        End If Next

                        End Sub

This article is part of the GWB Archives. Original Author: Scott Kuhl

Related Posts