Click here to Skip to main content
15,390,456 members
Articles / Programming Languages / Visual Basic
Posted 20 Sep 2002


28 bookmarked

CollapseAll Macro for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (15 votes)
20 Sep 2002
A simple macro to collapse all the project nodes in the Solution Explorer

Sample Image - CollapseAll.gif


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( _

    ' Check if there is any open solution
    If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
        ' MsgBox("Nothing to collapse. You must have an open solution.")
    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

    ' 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.

End Sub


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


About the Author

Edwin Evans
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
MemberDarren Boss2-Dec-09 5:31 
GeneralExcellent work, but DPack plugin for already do it(although slower than your macro solution) Pin
ZhaoRuFei23-Aug-07 16:04
MemberZhaoRuFei23-Aug-07 16:04 
GeneralVisual Studio 2005 Bug and Fix Pin
Scott Kuhl9-Apr-07 6:50
MemberScott Kuhl9-Apr-07 6:50 
GeneralRe: Visual Studio 2005 Bug and Fix Pin
Peter Solberg23-May-07 19:07
MemberPeter Solberg23-May-07 19:07 
GeneralVery useful, thanks! Pin
benno_119-Nov-06 13:31
Memberbenno_119-Nov-06 13:31 
GeneralConfigurable Depth Pin
DrewBurlingame14-Feb-06 11:35
MemberDrewBurlingame14-Feb-06 11:35 
Generallittle enhancment (you can publish it if you want) Pin
Bnaya Eshet12-Jan-06 4:53
MemberBnaya Eshet12-Jan-06 4:53 
GeneralGreat. Works with VS 2003 Pin
MtF_25-Jul-05 10:40
MemberMtF_25-Jul-05 10:40 
Generalexcellent, very useful :) Pin
feline_dracoform22-Jun-05 2:29
Memberfeline_dracoform22-Jun-05 2:29 
GeneralCollapse the subfolders too Pin
Charles Windhausen8-Dec-04 12:38
MemberCharles Windhausen8-Dec-04 12:38 
QuestionCan it work for ClassView? Pin
Kevin McFarlane4-Oct-04 6:29
MemberKevin McFarlane4-Oct-04 6:29 
AnswerRe: Can it work for ClassView? Pin
Erlend6-Feb-05 20:46
MemberErlend6-Feb-05 20:46 
GeneralAbout ToolButton Pin
Anonymous14-Nov-02 8:18
MemberAnonymous14-Nov-02 8:18 
GeneralRe: About ToolButton Pin
Edwin Evans14-Nov-02 18:28
MemberEdwin Evans14-Nov-02 18:28 
GeneralRe: About ToolButton Pin
Anonymous15-Nov-02 4:40
MemberAnonymous15-Nov-02 4:40 
GeneralRe: About ToolButton Pin
Booga28-Nov-02 4:13
MemberBooga28-Nov-02 4:13 
GeneralGreat Macro! Pin
Stephen Jones4-Oct-02 5:05
MemberStephen Jones4-Oct-02 5:05 
GeneralRe: Great Macro! Pin
Edwin Evans6-Oct-02 5:25
MemberEdwin Evans6-Oct-02 5:25 
GeneralRe: Great Macro! Pin
Booga23-Jan-03 2:41
MemberBooga23-Jan-03 2:41 
GeneralRe: Great Macro! Pin
Edwin Evans21-Jun-05 11:00
MemberEdwin Evans21-Jun-05 11:00 
GeneralRe: Great Macro! Pin
tcpc23-Jul-07 9:43
Membertcpc23-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.