Mouse Movement Through VBA

aadityajain08

New Member
Joined
Nov 20, 2018
Messages
9
Hi Team,

I have an requirement where i need to move the mouse cursor to to particular element on the screen. I tried to look the code but only able to find the code by setcursorposition where in you need to have the coordinates.
Is there any possibility that we can move mouse cursor without coordinates and by element value or name.

Alternatively, if above is not possible, then how can we find the coordinates of particular element on the screen. Also, if these coordinates are independent of the different desktop and laptop, screen resolutions. Because if i will hardcode the coordinates in my code and ran the code on different system then will it work or fail.

Please suggest.

Regards;
Aditya Jain
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi

Test this in a new workbook
- Paste code below into a standard module
- Insert a shape on the sheet
- Run the code
- Cursor moves to centre of the shape
- Modify to suit your reuirements


Code:
Option Explicit
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

Sub MoveCursorToShape()
    Dim x As Double, y As Double
    Dim AW As Object:       Set AW = ActiveWindow.Application.ActiveWindow
    Dim shp As Shape:       Set shp = ActiveSheet.Shapes(1)
    x = (shp.left + (shp.Width / 2)) / 0.75
    y = (shp.top + (shp.Height / 2)) / 0.75
    SetCursorPos AW.PointsToScreenPixelsX(x), AW.PointsToScreenPixelsY(y)
End Sub
 
Upvote 0
@aadityajain08

What you need depends on the "particular element on screen" ... What element on screen do you need to move the cursor to ? Is it within excel like a shape, cell, menu etc ... or outside excel ?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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