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

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.