API function FindWindowEx

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:

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 :biggrin:

EDIT: added the type declaration for hWndChild at Mark O'Brien's suggestion :biggrin:
 
Hi, I am fairly new to Windows API calls but I have been looking at this issue for a few days now. I've taken a good look at the code here and I am a bit puzzled. It appears that in the msgbox you are correctly identifying the First child of XLDESK in the strBuffer variable, but you have already pointed hWndChild to the next child just prior to calling MsgBox in the GetWindow function. Am I correct, or there something I am missing here? In the above code the Hex reference to the child window handle would be for the second child whereas the strBuffer would refer to the first.

With respect

Terry
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,215,494
Messages
6,125,139
Members
449,207
Latest member
VictorSiwiide

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top