65.9K
CodeProject is changing. Read more.
Home

CollapseAll Macro for Visual Studio .NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (14 votes)

Sep 21, 2002

viewsIcon

146768

A simple macro to collapse all the project nodes in the Solution Explorer

Sample Image - CollapseAll.gif

Introduction

Visual Studio .NET has a cool feature that it will automatically synchronize the Solution Explorer with the open file. One problem though is that if you have a large number of projects, the number of branches can soon get out of control - you can't see the trees for all the branches, so to speak. This is a very simple macro that you can use to collapse all the project nodes in the Solution Explorer.

Macro Code

    Sub CollapseAll()

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

        ' Check if there is any open solution
        If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
            ' MsgBox("Nothing to collapse. You must have an open solution.")
            Return
        End If

        ' Get the top node (the name of the solution)
        Dim UIHSolutionRootNode As UIHierarchyItem
        UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)

        ' Collapse each project node
        Dim UIHItem As UIHierarchyItem
        For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
            UIHItem.UIHierarchyItems.Expanded = False
        Next

        ' 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.
        UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)

    End Sub