Why won't simulated mouse click at X Y coordinates like a real mouse click?

chuckchuckit

Well-known Member
Joined
Sep 18, 2010
Messages
541
Is the only way to get a simulated mouse to click on the screen, is to go through window handles, setfocus, mouse_event etc?

Can't we somehow just use the same interrupt system the physical left button on the mouse uses when this left button is clicked by a user, and then use the mouse coordinates X Y on the screen just like the physical mouse system does?

Going through window handles etc is prone to many problems, and does not always click when it should such as applications with embedded menu codes.

Thanks.
Chuck
 
Last edited:
Sadly, seeing a picture won't actually help . I have a feeling you are not activating the program window properly. Have you tried using the SetCapture API before using the mouse_event API ?

Also, does the program expose a IDispatch interface if so then you could use the GetObject function and reach theprogram tree object. See if you can set a reference to the program Object Library.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Jaafar,
That sounds like a more promising direction. There may be something more needed in the mouse environment required for this application.

When I get some more time I'll try applying those suggestions, and will post back the progress/results.

Thanks.
 
Upvote 0
Jaafar, I tried adding the SetCapture API and it still does not simulate click that gear.

However, I should have mentioned that whenever the mouse pointer is over the gear, the gear turns a different color to let you know when the mouse pointer is in the clickable area for the gear.

Perhaps this hover feature which is interacting with the mouse is triggering system messages we may be able to access for some information we could use to simulate click the gear?

Still working on figuring how to look for the IDispatch and setting reference to the Object library.

Thanks.
 
Upvote 0
Still working on figuring how to look for the IDispatch and setting reference to the Object library.

Go to the VBE and go to Tools>References> look for any entries with the program name & the extensions dll, olb or ocx ....If you find any of them tick them to add a reference. If there are no entries click on the Browse button and search under the external program folder or the System32 folder.... If successful, (ie: you managed to add a reference) then Press F2 to access the VBE Object browser and choose the library from the Libraries dropdown.... This will show you any Interfaces,Classes and Functions that your external program exposes and which you could then manipulate from your VBA code.
 
Last edited:
Upvote 0
Jaafar, Sorry for not responding earlier to your kind efforts on this. I get very busy running a personal business at times, even though I would much prefer programming instead.

There are no entries with those 3 extensions in the VBA Tools>References. I carefully looked for possible abbreviations they may have used. Nothing in System32 either.

But in their program folder there are quite number of dll's in sub-folders. In sub-folder "usergui" are "jdic.dll" and "jWinAPI.dll".I tried adding these two and get the message "Can't add a reference to the specified file." This same message happens for any of the dll's in numerous subfolders. Did not find any olb or ocx.

So it appears there is no way to use VBA in this approach to manipulate their code.

Months ago I talked with the company about what I was trying to do, and they said simulated clicking could potentially be done with some of the mouse clicking software products sold, but they would not support any such interface. So it should be possible to do.

Perhaps I am still missing something in my simulation procedures and should post my code, unless you know of a different route to try first.

Thanks.
 
Upvote 0
run the mouse_event twice like in the code below and see what you get

Code:
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&

And also, how are you running your code to click the gear button ? Are you running it via a button or a key combination ? I am asking this because the way you run the calling code itself would have an impact on the mouse .
 
Upvote 0
Jaafar, Apologies once again for not responding earlier as my business has been so busy with major positive changes, and having to assist customers and new potential contract people etc. Resolving the automated mouse clicking here would certainly free us up more for these developing responsibilities. If you can share any additional input, I will have more time this week to respond with the results to your appreciated efforts.

I tried your double click suggestion in post #16. And tried starting macro via both a key combo option, and also from Excel menu, view macro, run. The latter I assume is the more reliable way? Still no success. I usually just click my code and push F5 to test.

My code below works to simulate a mouse click in most applications. However in the problematic application it does show focus somewhat (certain screen areas may change color), but as you have stated, it does not seem to be setting the focus properly to properly click the that gear button. I am now convinced that is the problem with trying to click this application.

Code I am trying:
Code:
Dim MyPointAPI As POINTAPI
Private Type POINTAPI
  x As Long
  y As Long
End Type

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

Private Declare Function WindowFromPoint Lib "user32" ( _
 ByVal PointX As Long, _
 ByVal PointY As Long _
 ) As Long
 
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long

Private 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)

Const MOUSEEVENTF_LEFTDOWN As Integer = 2
Const MOUSEEVENTF_LEFTUP As Integer = 4

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long
    
Private Const WM_SETFOCUS = &H7

Sub MouseSimulateClick()
    Dim OurMouse_X As Long, OurMouse_Y As Long, Gear_WinHndl As Long, Rtn As Long

    'X Y screen location of gear to click
    OurMouse_X = 100
    OurMouse_Y = 200

    Call SetCursorPos(OurMouse_X, OurMouse_Y)  'mouse move to gear location
    Gear_WinHndl = WindowFromPoint(OurMouse_X, OurMouse_Y) 'window handle
    Rtn = SetForegroundWindow(Gear_WinHndl) 'activate window
    Rtn = PostMessage(Gear_WinHndl, WM_SETFOCUS, 0&, 0&) 'MSDN says this will ensure better window focus for mouse
    DoEvents
    Rtn = SetCapture(Gear_WinHndl)
    DoEvents
    mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
    mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
    DoEvents
    Rtn = ReleaseCapture
End Sub
 
Upvote 0
It looks like where the code is failing is on the below line, (due to it returns 0):
Code:
Rtn = PostMessage(Gear_WinHndl, WM_SETFOCUS, 0&, 0&)
I think the reason is due to external application from Excel I am trying to click has its own independent states including "focus". It's input processing thread needs to be attached to the calling thread (Excel), which I think can be done through API function "AttachThreadInput". And then released when done.
 
Upvote 0
Nope - "AttachThreadInput" fails only with this application for some reason, and per MSDN it only fails for two reasons, 1. doesn't have a message queue (which it likely does have a queue), 2. "...a journal record hook is installed. Journal record hooks attach all input queues together." Not sure how to verify that #2.

Really getting stumped on this one so far...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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