Okay, so this is not exactly what you want, because he will not do it as the default, but I think he is as close to him as possible, and it is pretty elegant, so here.
So, I know the most amazing MZTools ( http://www.mztools.com/v3/mztools3.aspx ) has the ability to collapse all nodes in the tree structure. So I was looking a bit to find out how to manage the visual presentation of a VBA project through Win32API. Most of the API calls I received from this page are: http://btmtz.mvps.org/treeview/ (latest update 1999!).
From there it was just a matter of getting the right pens and the right checks. Note. The VBIDE window must be open (it does not have to be visible) for this to work. I suggest creating a toolbar in VBIDE and activating it if necessary.
It works for me in Office 2007/2010 32Bit on Windows 7, you will have to modify Win32API for 64Bit, but that would not be easy. You can also specify collapse / extension for specific projects, etc. Based on what you need.
This is a continuation:
Sub CollapseVBIDETree() Dim hwndVBIDE As Long, hwndVBAProj As Long, hwndTV As Long Dim hwndCurrent As Long, hwndChildCurrent As Long Dim bSuccessModule As Boolean, bSuccessElse As Boolean, sNodeName As String 'Find the handle of the VBEIDE window, down to the treeview in the project window hwndVBIDE = FindWindow("wndclass_desked_gsk", vbNullString) 'VBIDE Window hwndVBAProj = FindWindowEx(hwndVBIDE, 0&, "PROJECT", vbNullString) 'The Project - VBAProject Window hwndTV = FindWindowEx(hwndVBAProj, 0&, "SysTreeView32", vbNullString) 'The Treeview in the VBAProject Window 'Get the handle of the Root of the Treeview hwndCurrent = TreeView_GetRoot(hwndTV) 'Loop through all the children of the treeview. This is all the current VBA Projects. 'We can loop through until there are none left and a handle of zero is return Do While hwndCurrent <> 0 sNodeName = GetTVItemText(hwndTV, hwndCurrent) 'Get the first child in the current project which is the 'Microsoft Excel Objects' hwndChildCurrent = TreeView_GetChild(hwndTV, hwndCurrent) 'Set up a boolean to check if there is a 'Modules' child. If not, we'll collapse the whole project bSuccessModule = False 'Loop through all the child nodes to find the 'Modules' node Do While hwndChildCurrent <> 0 'Get the name of the node sNodeName = GetTVItemText(hwndTV, hwndChildCurrent) 'If we find the Modules node then Expand it and flag it If sNodeName = "Modules" Then bSuccessModule = TreeView_Expand(hwndTV, hwndChildCurrent, TVE_EXPAND) Else 'Otherwise collapse it bSuccessElse = TreeView_Expand(hwndTV, hwndChildCurrent, TVE_COLLAPSE) End If hwndChildCurrent = TreeView_GetNextSibling(hwndTV, hwndChildCurrent) Loop 'If we don't find a Modules child then collapse the entire branch for that project If Not bSuccessModule Then Call TreeView_Expand(hwndTV, hwndCurrent, TVE_COLLAPSE) Else 'Some workbooks if collapsed would stay collapsed so make sure they are expanded Call TreeView_Expand(hwndTV, hwndCurrent, TVE_EXPAND) End If 'Move onto the next project hwndCurrent = TreeView_GetNextSibling(hwndTV, hwndCurrent) Loop End Sub
And these are the Win32API declarations:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _ ByVal hWndParent As Long, _ ByVal hWndChildAfter As Long, _ ByVal lpszClassName As String, _ ByVal lpszWindowName As String) As Long Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ wParam As Any, _ lParam As Any) As Long ' =========================================================================== ' treeview definitions defined in Commctrl.h at: ' http://premium.microsoft.com/msdn/library/sdkdoc/c67_4c8m.htm Public Type TVITEM ' was TV_ITEM mask As Long hItem As Long State As Long stateMask As Long pszText As String ' Long ' pointer cchTextMax As Long iImage As Long iSelectedImage As Long cChildren As Long lParam As Long End Type ' Public Enum TVITEM_mask TVIF_TEXT = &H1 TVIF_IMAGE = &H2 TVIF_PARAM = &H4 TVIF_STATE = &H8 TVIF_HANDLE = &H10 TVIF_SELECTEDIMAGE = &H20 TVIF_CHILDREN = &H40 #If (Win32_IE >= &H400) Then ' WIN32_IE = 1024 (>= Comctl32.dll v4.71) TVIF_INTEGRAL = &H80