CollapseAllMacroForVS

August 05, 2010
I swiped these from the web and edited these - so if anyone's pissed that I've glommed their stuff - lemme know.

The 2005 version is slightly different from 2008, in that it only collapses nodes that are expanded. If a parent node itself is collapsed, but has many expanded nodes underneath, those underneath nodes are skipped. The 08 version collapses all nodes regardless, and therefore is a little bit cleaner if you really want ALL nodes collapsed.

The reason for the difference is that the 2008 version run in 2005 acts weird - as if it first expands each child node being traveled to - so even if all nodes are collapsed when the macro is run, it does a bunch of expanding of nodes, then collapses. The end result is the same, but too much noise and delay in the meantime.

The 2005 version is quite a bit faster than the 2008 version - so if only care about top nodes being collapsed, put the ‘05 version in 2008.

(The 2008 version appears to work fine in 2010, too)

(Power Tools for 2010 also will do this)

2005:
Public Module Module1

Sub CollapseMe(ByVal oRootItem As UIHierarchyItem)

Dim oChildItem As UIHierarchyItem

If (oRootItem.UIHierarchyItems.Expanded = True) Then

For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
Next

oRootItem.UIHierarchyItems.Expanded = False

End If

End Sub

'------------------------------------------------------------------------------
Sub CollapseAll()
'DESCRIPTION: Colapse all the nodes in the project tree

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

' Check if there is any open solution
If (oSolutionExplorer.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 oRootItem As UIHierarchyItem
oRootItem = oSolutionExplorer.UIHierarchyItems.Item(1)
Dim oChildItem As UIHierarchyItem

' Collapse each project node
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
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.
oRootItem.Select(vsUISelectionType.vsUISelectionTypeSelect)

End Sub

End Module


2008:
Public Module Module1

Sub CollapseMe(ByVal oRootItem As UIHierarchyItem)

Dim oChildItem As UIHierarchyItem

For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
Next

oRootItem.UIHierarchyItems.Expanded = False

End Sub

'------------------------------------------------------------------------------
Sub CollapseAll()
'DESCRIPTION: Colapse all the nodes in the project tree

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

' Check if there is any open solution
If (oSolutionExplorer.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 oRootItem As UIHierarchyItem
oRootItem = oSolutionExplorer.UIHierarchyItems.Item(1)
Dim oChildItem As UIHierarchyItem

' Collapse each project node
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
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.
oRootItem.Select(vsUISelectionType.vsUISelectionTypeSelect)

End Sub

End Module


tags: ComputersAndTechnology