Hello,
I'am beginner in VBA and I need some help please.
I try to handle an external application with vba macro, and actually I need to push a button on this application. The application name is Axel, and I am traying to do this with mouse_event, so the idea is to postion the cursor on the right button then launch a mouse-event leftdown and leftup. But the problem is that the click is happened before that application is opened on the screen so it clicks somewhere else on the screen. Can you please give a help?
This is my code:
Option Explicit
Private Declare Function GetDesktopWindow Lib "User32" () As Long
Private Declare Function GetWindow& Lib "User32" (ByVal hWnd&, ByVal wCmd&)
Private Declare Function GetWindowLong& Lib "User32" _
Alias "GetWindowLongA" (ByVal hWnd&, ByVal nIndex&)
Private Declare Function GetWindowText& Lib "User32" Alias _
"GetWindowTextA" (ByVal hWnd&, ByVal lpString$, ByVal aint&)
Private Const mconMAXLEN = 255
Public Declare Function SetCursorPos Lib "User32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "User32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
Sub AxelActivate()
Dim hWnd&, Style&, Title$, i&
Dim Nom As String
Dim hWndApp, hWndButton As Long
hWnd = GetWindow(GetDesktopWindow(), 5)
Do While hWnd
Title = GetCaption(hWnd)
If Len(Title) Then
If GetWindowLong(hWnd, -16) And &H10000000 Then
If InStr(1, Title, "Axel", 1) Then
AppActivate Title
SendKeys "~"
Exit Sub
End If
End If
End If
hWnd = GetWindow(hWnd, 2)
SetCursorPos 60, 200 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Loop
MsgBox "Axel is not opened!", vbCritical, "ERROR"
End Sub
Private Function GetCaption(hWnd&) As String
Dim i%, Buffer$: Buffer = String$(254, 0)
i = GetWindowText(hWnd, Buffer, 255)
If i Then GetCaption = Left$(Buffer, i)
End Function
Also if someone has an other idea to click on a button with only vba It will be a great thing.
Thank you
Bookmarks