?
Solved

Controlling mouse with VBA

Posted on 2010-04-01
4
Medium Priority
?
491 Views
Last Modified: 2013-12-20
Is it possible to control the mouse with VBA command, reference or an activex control ?
I mean both moving and clicking.
0
Comment
Question by:Crawler77
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 29385829
Sure...you can use mouse_event() or the newer SendInput():

Option Explicit

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

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

0
 
LVL 14

Expert Comment

by:RDWaibel
ID: 29385941
Are you talking about automating the mouse to move with out ANY user intervention?
0
 

Author Comment

by:Crawler77
ID: 29440922
Thanks very much Idle_Mind.
That was EXACTLY what I asked for.
A well deserved 500 points goes to you :)
0
 

Author Comment

by:Crawler77
ID: 29441019
Yes RDWaibel I was asking for that. And above code works perfect.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month3 days, 21 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question