Click here to Skip to main content
15,892,575 members
Articles / Programming Languages / Visual Basic
Article

CollapseAll Macro for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (15 votes)
20 Sep 2002 145K   28   21
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

VBScript
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Edwin Evans lives with his wife and daughter in Santa Clara, California. He is the creator of HandyFind, a faster, friendlier way to find words in documents and Web pages. Edwin is also working to help create the technological Singularity by supporting the Singularity Institute for Artificial Intelligence.

Comments and Discussions

 
GeneralGreat Macro! Pin
Darren Boss2-Dec-09 5:31
Darren Boss2-Dec-09 5:31 
GeneralExcellent work, but DPack plugin for vs.net already do it(although slower than your macro solution) Pin
ZhaoRuFei23-Aug-07 16:04
ZhaoRuFei23-Aug-07 16:04 
GeneralVisual Studio 2005 Bug and Fix Pin
Scott Kuhl9-Apr-07 6:50
Scott Kuhl9-Apr-07 6:50 
GeneralRe: Visual Studio 2005 Bug and Fix Pin
Peter Solberg23-May-07 19:07
Peter Solberg23-May-07 19:07 
GeneralVery useful, thanks! Pin
benno_119-Nov-06 13:31
benno_119-Nov-06 13:31 
GeneralConfigurable Depth Pin
DrewBurlingame14-Feb-06 11:35
DrewBurlingame14-Feb-06 11:35 
Generallittle enhancment (you can publish it if you want) Pin
Bnaya Eshet12-Jan-06 4:53
Bnaya Eshet12-Jan-06 4:53 
GeneralGreat. Works with VS 2003 Pin
MtF_25-Jul-05 10:40
MtF_25-Jul-05 10:40 
Generalexcellent, very useful :) Pin
feline_dracoform22-Jun-05 2:29
feline_dracoform22-Jun-05 2:29 
GeneralCollapse the subfolders too Pin
Charles Windhausen8-Dec-04 12:38
Charles Windhausen8-Dec-04 12:38 
QuestionCan it work for ClassView? Pin
Kevin McFarlane4-Oct-04 6:29
Kevin McFarlane4-Oct-04 6:29 
AnswerRe: Can it work for ClassView? Pin
Erlend6-Feb-05 20:46
Erlend6-Feb-05 20:46 
GeneralAbout ToolButton Pin
Anonymous14-Nov-02 8:18
Anonymous14-Nov-02 8:18 
GeneralRe: About ToolButton Pin
Edwin Evans14-Nov-02 18:28
Edwin Evans14-Nov-02 18:28 
GeneralRe: About ToolButton Pin
Anonymous15-Nov-02 4:40
Anonymous15-Nov-02 4:40 
GeneralRe: About ToolButton Pin
Booga28-Nov-02 4:13
Booga28-Nov-02 4:13 
GeneralGreat Macro! Pin
Stephen Jones4-Oct-02 5:05
Stephen Jones4-Oct-02 5:05 
GeneralRe: Great Macro! Pin
Edwin Evans6-Oct-02 5:25
Edwin Evans6-Oct-02 5:25 
GeneralRe: Great Macro! Pin
Booga23-Jan-03 2:41
Booga23-Jan-03 2:41 
It was great, but not quite what I was looking for. Here's my modifications, which allow the macro to collapse all subfolders of expanded folders as well. It does not deal with expanded subfolders in collapsed folders, as that takes much too long (it actually seemed to hang my VS).
Enjoy!

Sub CollapseNode(ByRef item As UIHierarchyItem)

Dim subitem As UIHierarchyItem
For Each subitem In item.UIHierarchyItems
If (subitem.UIHierarchyItems.Expanded = True) Then
CollapseNode(subitem)
subitem.UIHierarchyItems.Expanded = False
End If
Next

End Sub

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)

CollapseNode(UIHSolutionRootNode)

' 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
GeneralRe: Great Macro! Pin
Edwin Evans21-Jun-05 11:00
Edwin Evans21-Jun-05 11:00 
GeneralRe: Great Macro! Pin
tcpc23-Jul-07 9:43
tcpc23-Jul-07 9:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.