vba to control mouse on screen incl. applications outside Excel

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
683
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I am trying to use vba to control mouse movements and clicks on screen. As a starting point I want to just minimise Excel then minimise the application visible after Excel is minimised but the code I've got so far won't do this - it minimises Excel ok but then won't do a further minimise of the next visible application on screen. I have two chunks of code as follows:

VBA Code:
Option Explicit

#If VBA7 Then
Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As LongPtr)
#Else
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)
#End If

Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Const MOUSEEVENTF_RIGHTUP As Long = &H10
Dim TimerActive As Boolean

And then I run this macro:

Code:
SetCursorPos 1804, 11 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
 
Application.Wait Now + TimeSerial(0, 0, 2)

SetCursorPos 1828, 6 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

Any help much appreciated.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
As far as I am aware you can't use mouse events in another application.

But you can can send key presses to other applications. If you send the following two keystrokes to an app:
Alt-Space
n
then it will minimise the app.

To read more about this, search on "VBA sendkeys"
 
Upvote 0
Programmatically interacting with applications using mouse clicks or keystrokes blindly won't get you far.

Depending on what you want to achieve with the mouse clicks out of the target application(s) , there are better approaches such as using MSAA, UI Automation, OLE automation if supported or simply Sending WIN32 messages to the target application.

If I just wanted to minimize an application, I would first obtain the handle to the main window (hwnd) and then send to it the WM_SYSCOMMAND message along with SC_MINIMIZE in the wParam parameter using the SendMessage API.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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