1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| Option Explicit
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Const strConst As String = "mot spécial"
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDLAST = 1
Private Const GW_HWNDNEXT = 2
Private Const GW_HWNDPREV = 3
Private Const GW_OWNER = 4
Private Const GW_CHILD = 5
Private Const BM_CLICK = &HF5
Private Const WM_CLOSE = &H10
Private Sub Form_Load()
Me.TimerInterval = 2000
Me.OnTimer = "=TrackMsgBox()"
End Sub
Private Function TrackMsgBox()
Dim h0 As Long, h1 As Long, h2 As Long
' trouve le handle d'une msgbox Excel
h0 = FindWindowLike(0, "Excel", "#32770") 'MsgBox générée par Excel
If h0 <> 0 Then
' trouve le Handle du texte recherché
h1 = FindWindowLike(h0, strConst, "Static") 'Cherche un mot particulier sur le msgbox
If h1 <> 0 Then
' trouve le handle du bouton Yes
h2 = FindWindowLike(h0, "&Yes", "button") 'Bouton Yes de la message Box
Debug.Print "Click YES envoyé sur " & h2 & " @" & Format(Now, "hh:nn:ss")
' click sur le bouton
SendMessage h2, BM_CLICK, 0, 0& 'envoie un click
Else
Exit Function
End If
Else
Exit Function
End If
End Function
Private Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, _
ClassName As String) As Long
Dim hwnd As Long
Dim sWindowText As String
Dim sClassname As String
Dim r As Long
Static level As Integer
If level = 0 Then
If hWndStart = 0 Then hWndStart = GetDesktopWindow()
End If
level = level + 1
hwnd = GetWindow(hWndStart, GW_CHILD)
Do Until hwnd = 0
Call FindWindowLike(hwnd, WindowText, ClassName)
sWindowText = Space$(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
sClassname = Space$(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)
If (sWindowText Like "*" & WindowText & "*") Then
If (sClassname = ClassName) Then
FindWindowLike = hwnd
Exit Do
End If
End If
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
level = level - 1
End Function |
Partager