dimanche 19 avril 2015

Excel 2013 Windows Class Names

Have to learn a little API for my VBA project so am experimenting. The interwebs suggests this code should work in a sub to find a windowshandle



Private Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

Public Sub Test()
Dim P As String
P = Windows(1).Caption
MsgBox P
MsgBox FindWindowA("", P)
End Sub


With a sheet open with caption "Book1.xlsm" when I run this code I get "Book1.xlsm" but then "0"


What am I doing wrong? If I try "EXCEL7" as a class name instead of the null string, I get the same thing.


Thanks to those older and wiser


Added later after discussion below.... This isn't really an answer to my question as it's not clear on the face of it why windows can't be found via FindWindowA (that's what it claims to do, as I read it) but further research suggests that I can't get the window handles directly via FindWindowA, but have to take into account that they might be child windows. So this code at least find my window handle:



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
Dim hWndParent As Long, hWndDskTop As Long, hWndChild As Long, hWndMine As Long
Sub Test()
hWndParent = FindWindow("XLMAIN", vbNullString)
hWndDskTop = FindWindowEx(hWndParent, 0&, "XLDESK", vbNullString)
hWndMine = FindWindowEx(hWndDskTop, 0&, "EXCEL7", vbNullString)
MsgBox hWndMine


That is finding the window handle of Book1.xlsm as I originally wanted.


Aucun commentaire:

Enregistrer un commentaire