vbaapifindwindow

VBA - Find a window handle knowing its caption, class and its owner


It should be easy but it doesn't work in the way I imagined... It is about "Print Merge Wizard" window of Corel DRAW. I can find the window with Spy++, its Caption and its Class. I cannot find its handle using API. Using FindWindow I can only obtain the main Corel Window:

TxtHwnd = FindWindow("CorelDRAW18", vbNullString)

When I try the same way, using the caption and the class, it cannot return the handle:

hMerge = FindWindow("#32770 (Dialog)", "Print Merge Wizard")

I tried using of FindWindowEx in the next way, but without success:

TxtHwnd = FindWindow("CorelDRAW18", vbNullString): Debug.Print TxtHwnd
hMerge = FindWindowEx(TxtHwnd, ByVal CLngPtr(0), "#32770 (Dialog)", "Print Merge Wizard")

I also tried to iterate through all open windows using:

    Sub ListWins(Optional Title = "*XYZ*", Optional Class = "*")
    Dim hWndThis As Long
    hWndThis = FindWindow(vbNullString, vbNullString)
    While hWndThis
        Dim sTitle As String, sClass As String
        sTitle = Space$(255)
        sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))
        sClass = Space$(255)
        sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))
        If sTitle Like Title And sClass Like Class Then
            Debug.Print sTitle, sClass
            Stop
        End If
        hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
        Debug.Print sTitle, sClass, hWndThis
    Wend 
End Sub

Calling the function like this:

ListWins "Print Merge Wizard", "#32770 (Dialog)"

The window in discussion is not between the ones showed by the code... Looking into Spy++ window properties I can see the Print Merge Wizard is not a Corel child window. It just shows Corel like being its owner...

Knowing all that, in which way do you think I can catch the Print Merge Wizard Window handler?

Thanks in advance!


Solution

  • It strangely works using FindWindow without class name, even if the class name was correct:

    hwndWiz = FindWindow(vbNullString, "Print Merge Wizard") returns the right handler.