Richard Schollar
MrExcel MVP
- Joined
- Apr 19, 2005
- Messages
- 23,707
Hi
I was playing around with the this function trying to get a handle on a workbook window. I understood that the Windows Class Name of the workbook window was "EXCEL7" and the class name of the top-most level was "XLMAIN" (the handle to this topmost level can most easily be found thru Application.Hwnd, but I was trying things out!).
Following declaring the APIs and writing a bit of code, I thought the following would work to determine the handle to the workbook window:
But this didn't work, and I kept getting a return value of 0 indicating that the function was not finding the handle.
Subsequent playing around and using Winspector I noticed that there is an 'Intermediate' window between XLMAIN and EXCEL7 - this being XLDESK. Hence, the above code could be made to work by first determining the handle to this Intermediate window and using this handle as the parent handle in the FindWindowEx to return the Book1 Window:
So it would appear that the FindWindowEx only finds 'immediate' childs of the Parent, and doesn't delve deeper into the Windows parent-child structure.
Anyway, my questions are thus:
1. Is my thinking on this point correct?
2. Is there no way of determining the handle of a subsequent child that may not be an immediate child without knowing the parent-child structure of all preceding windows?
Thanks for all replies
EDIT: added the type declaration for hWndChild at Mark O'Brien's suggestion
I was playing around with the this function trying to get a handle on a workbook window. I understood that the Windows Class Name of the workbook window was "EXCEL7" and the class name of the top-most level was "XLMAIN" (the handle to this topmost level can most easily be found thru Application.Hwnd, but I was trying things out!).
Following declaring the APIs and writing a bit of code, I thought the following would work to determine the handle to the workbook window:
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private 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
Sub Test()
Dim hWndParent As Long, sParentClassName As String
Dim hWndChild As Long, sChildClassName As String, sChildWindow As String
sParentClassName = "XLMAIN"
hWndParent = FindWindow(sParentClassName, vbNullString)
'Application.hWnd returns the same as above
sChildClassName = "EXCEL7"
sChildWindow = ActiveWindow.Caption 'in my case "Book1"
hWndChild = FindWindowEx(hWndInt, ByVal 0&, sChildClassName, sChildWindow)
MsgBox hWndChild 'this returns 0 ie the function didn't find the handle
End Sub
But this didn't work, and I kept getting a return value of 0 indicating that the function was not finding the handle.
Subsequent playing around and using Winspector I noticed that there is an 'Intermediate' window between XLMAIN and EXCEL7 - this being XLDESK. Hence, the above code could be made to work by first determining the handle to this Intermediate window and using this handle as the parent handle in the FindWindowEx to return the Book1 Window:
Code:
'Declarations as above
Sub Test()
Dim hWndParent As Long, sParentClassName As String
Dim hWndInt As Long, sClassInt As String
Dim hWndChild As Long, sChildClassName As String, sChildWindow As String
sParentClassName = "XLMAIN"
hWndParent = FindWindow(sParentClassName, vbNullString)
sClassInt = "XLDESK"
hWndInt = FindWindowEx(hWndParent, ByVal 0&, sClassInt, vbNullString)
sChildClassName = "EXCEL7"
sChildWindow = ActiveWindow.Caption
hWndChild = FindWindowEx(hWndInt, ByVal 0&, sChildClassName, sChildWindow)
MsgBox hWndChild 'success!!!
End Sub
So it would appear that the FindWindowEx only finds 'immediate' childs of the Parent, and doesn't delve deeper into the Windows parent-child structure.
Anyway, my questions are thus:
1. Is my thinking on this point correct?
2. Is there no way of determining the handle of a subsequent child that may not be an immediate child without knowing the parent-child structure of all preceding windows?
Thanks for all replies

EDIT: added the type declaration for hWndChild at Mark O'Brien's suggestion
