Controlling mouse with VBA

Is it possible to control the mouse with VBA command, reference or an activex control ?
I mean both moving and clicking.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented: can use mouse_event() or the newer SendInput():

Option Explicit

Private Type POINTAPI
    x As Long
    y As Long
End Type


Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) 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)

Private Sub LeftClick(ByVal x As Long, ByVal y As Long)
    ' store the current position
    Dim startPos As POINTAPI
    GetCursorPos startPos
    ' set the desired position
    SetCursorPos x, y
    ' left click
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
    ' restore the original position
    SetCursorPos startPos.x, startPos.y
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Are you talking about automating the mouse to move with out ANY user intervention?
Crawler77Author Commented:
Thanks very much Idle_Mind.
That was EXACTLY what I asked for.
A well deserved 500 points goes to you :)
Crawler77Author Commented:
Yes RDWaibel I was asking for that. And above code works perfect.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.