Connect to the SysTreeView32 child class of the VBE window

I am new to WinApi calls, although I am familiar with VBA. I am trying to connect to the SysTreeView32 child class of a VBE window (Project Explorer TreeView). I would like to expand / collapse the elements of the tree structure by changing the registry keys (or, alternatively, by sending mouse clicks (mouse_event), although I prefer the first option).
I can find the main Excel window using this code:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Sub Find_Window() Dim hWndExcel As Long hWndExcel = FindWindow("XLMAIN", Application.Caption) MsgBox hWndExcel End Sub 

With Window Detective, I can access names, properties, etc. child classes.
Window Detective child classes
But I can’t decide how to get / activate (or even return the HWID) of the SysTreeView32 child class to collapse / expand items (folders). I'm not sure how to iterate over the elements, but then I will study them. The problem here is accessing the SysTreeView32 class. How can I achieve this?

When I try msgbox it
FindWindow("wndclass_desked_gsk", Application.Caption)
or
FindWindow("SysTreeView32", Application.Caption)
a 0 returned, so I'm obviously doing something wrong: / Thank you for your time.

+2
source share
2 answers

you should use:

 application.vbe.mainwindow.caption 

here is an example of code collapse code

 Private Const TVE_COLLAPSE = &H1 Private Const TVE_COLLAPSERESET = &H8000 Private Const TVE_EXPAND = &H2 Private Const TVE_EXPANDPARTIAL = &H4000 Private Const TVE_TOGGLE = &H3 Private Const TV_FIRST = &H1100 Private Const TVM_EXPAND = (TV_FIRST + 2) Private Const TVM_GETNEXTITEM = (TV_FIRST + 10) Private Const TVGN_ROOT = &H0 Private Const TVGN_NEXTVISIBLE = &H6 Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Sub CollapseProjects() Dim hWndVBE As Long, hWndPE As Long, hWndTvw As Long, hNode As Long, varReturn hWndVBE = FindWindowEx(0, 0, "wndclass_desked_gsk", Application.VBE.MainWindow.Caption) hWndPE = FindWindowEx(hWndVBE, 0, "PROJECT", vbNullString) hWndTvw = FindWindowEx(hWndPE, 0, "SysTreeView32", vbNullString) hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_ROOT, 0&) Do While hNode <> 0 varReturn = SendMessage(hWndTvw, TVM_EXPAND, TVE_COLLAPSE, hNode) hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, hNode) Loop End Sub 

in addition to your comment, here is the code to collapse only Microsoft Excel Objects nodes

 Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Declare Function SendMessageB Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long Private Const MAX_ITEM As Long = 256 Private Const TV_FIRST As Long = &H1100 Private Const TVM_EXPAND As Long = (TV_FIRST + 2) Private Const TVM_GETNEXTITEM As Long = (TV_FIRST + 10) Private Const TVM_GETITEM As Long = (TV_FIRST + 12) Const TVE_COLLAPSE As Long = &H1 Const TVE_EXPAND As Long = &H2 Private Const TVGN_ROOT As Long = &H0 Private Const TVGN_NEXT As Long = &H1 Private Const TVIF_TEXT As Long = &H1 Private Const TVGN_NEXTVISIBLE = &H6 Private Type TVITEM ' was TV_ITEM mask As Long hItem As Long state As Long stateMask As Long pszText As String cchTextMax As Long iImage As Long iSelectedImage As Long cChildren As Long lParam As Long End Type Sub CollapseXLObjects() Dim hWndVBE As Long Dim hWndPE As Long Dim hWndTvw As Long Dim hNode As Long Dim tvi As TVITEM Dim nChild As Long Dim sText As String Dim varReturn hWndVBE = FindWindowEx(0, 0, "wndclass_desked_gsk", Application.VBE.MainWindow.Caption) hWndPE = FindWindowEx(hWndVBE, 0, "PROJECT", vbNullString) hWndTvw = FindWindowEx(hWndPE, 0, "SysTreeView32", vbNullString) hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_ROOT, 0&) Do While hNode <> 0 tvi.hItem = hNode tvi.mask = TVIF_TEXT tvi.cchTextMax = MAX_ITEM tvi.pszText = String(MAX_ITEM, 0) nChild = SendMessageB(hWndTvw, TVM_GETITEM, 0&, tvi) If InStr(1, tvi.pszText, "Microsoft Excel Objects", vbTextCompare) > 0 Then varReturn = SendMessage(hWndTvw, TVM_EXPAND, TVE_COLLAPSE, hNode) Else varReturn = SendMessage(hWndTvw, TVM_EXPAND, TVE_EXPAND, hNode) End If hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, hNode) Loop End Sub 
+6
source

If you call this sub from another sub (in my case, sub in XL addin associated with the user button that opens VBE), and another module is highlighted (which may be in another project), then I found

  Application.VBE.MainWindow.Caption 

does not work. To capture the highlighted module, I use:

  Private Sub VisualBasicEditor() On Error Resume Next WinName = "Microsoft Visual Basic - " + ActiveWorkbook.Name + " [Running] - [" + Application.VBE.ActiveCodePane.CodeModule.Name + " (Code)]" Application.VBE.MainWindow.Visible = True Call CollapseXLObjects End Sub 

and in a module containing Sub CollapseXLObjects declare

  Public WinName As String 

and in Sub CollapseXLObjects

  hWndVBE = FindWindowEx(0, 0, "wndclass_desked_gsk", WinName) 
0
source

All Articles