Quick Jump
Introduction | Example | Tutorial | Applications | Comments

Introduction - Mouse Control

Did you know you could control your mouse with a VBA macro? Among other things, the user32 library (user32.dll) allows you to right-click, left-click and change your cursor position. Keep reading to find out how you can use mouse controls to create hands-free MS Paint art.

This article teaches you how to programmatically control your mouse with VBA. Instead of controlling your mouse manually, would you like to record your mouse and have it instantly converted to a VBA macro? I made an Excel add-in for that. Check out Mouse to Macro - it’ll save you hours of programming!

Example

Create MS Paint Art with Mouse Control Macro

'Declare mouse events
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)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
'Declare sleep
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub CityscapeSkyline()
'Open MS Paint and select Natural pencil Brush with 6px width
For k = 1 To 3
  SetCursorPos 16, 500
  Sleep 50
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  For i = 16 To 600 Step 5
    For j = 500 To 300 Step -Int((180 - 10 + 1) * Rnd + 10)
      SetCursorPos i, j
      Sleep 10
    Next j
  Next i
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Next k
End Sub

Your time is valuable. It's time to become a VBA expert.

Tutorial

How to automatically create your own skyline art

Want to create a city skyline? Follow these steps to create your own art:

  1. Open your Excel VBA Editor
  2. Open MS Paint
  3. Place your VBA editor on the right side of your screen and your MS Paint window on the left, like so:
    MS Paint and VBA Editor Side by Side

  4. Create a New Module in your VBA Editor
  5. Paste the Mouse Control Example into your new Module
  6. Select the Natural pencil brush in MS Paint
    MS Paint Natural Pencil

  7. Change your brush width to 6px
    MS Paint Natural Pencil 6px

  8. Execute the CityscapeSkyline Macro, sit back, and watch your automatic artist to its work!

Here’s the skyline art that was created when I ran my macro.

MS Paint Skyline Art with VBA Macro
MS Paint Skyline Art with VBA Macro

Looks like a big city, eh? The beauty of this macro is that the heights of the buildings are random. The Step declaration Step -Int((180 - 10 + 1) * Rnd + 10) randomly moves the mouse position up from 10 pixels to 180 pixels until it’s less than 300 pixels from the top of your screen. Each time you run the macro, your skyline will be different.

Practical VBA Mouse Control

Sure, that was pretty cool and all but it wasn’t very practical, was it? Here, I’ll try to explain how to move your mouse, left-click and right-click. I’ll also present some best practices regarding the Sleep function. Remember to paste the following code at the top of your module before trying these examples:

'Declare mouse events
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)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
'Declare sleep
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Move your Mouse

The SetCursorPos line positions your mouse cursor relative to the top left of your screen (position 0, 0). For example, SetCursorPos 25, 100 moves your mouse 25 pixels from the left of your screen and 100 pixels down from the top of your screen.

Left-click

The left-click down and left-click up buttons are independent. This gives you the ability to “hold” your mouse down while you move it around, like you would to highlight text. However, if you want to left-click without the hold, paste the following subroutine into your VBA editor and call it ( Call LeftClick ) from a different subroutine:

Private Sub LeftClick()
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  Sleep 50
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

What if you need to double-click? Just call the macro twice!

Right-click

Like the left-click, the right mouse down and mouse up clicks are independent. Here’s a sample of a routine right-click event:

Private Sub RightClick()
  mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
  Sleep 50
  mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub

64-Bit Mouse Control

To control your mouse on a 64-bit machine, modify the two Public Declare statements as follows:

Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As LongPtr
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 Long)

Importance of Sleep

The Sleep line delays your code for the number of milliseconds you declare. For example, Sleep 5000 will pause your code for 5 seconds.

Without the Sleep command, your computer may not register your mouse clicks or mouse movements. In other words, Sleep makes your mouse control macros more reliable.

Application Ideas

It’s a good idea to make sure your window will be placed in the same spot before running mouse control macros. One way to do this is to use vba to maximize your window.

Want to earn a high score on that computer game you like so much? Automate it! Whether you like it or not, I’ve used mouse control to set records on several flash-based web games. Hey, it’s funner to write a VBA code to play the game than it is to actually play some of those games!

I also use mouse controls to automate many of my routine tasks at my nuclear engineering job. A while back, I wrote a macro to open and fill out my timesheet. I just press a button and watch my computer do my work. If you need help automating your routine tasks at work, let me know and I’ll be happy to help! I’m all about increasing efficiency.

If you’re looking to develop your fundamentals, practice drawing basic geometric shapes in Paint. I started learning VBA mouse controls by painting squares.

Comments

If you like what you see, subscribe to my email list, share this article with a friend, submit a comment below and follow me on Google+! Come back often to see more great VBA ideas.

Remember, if you or your company are struggling with VBA, just contact me and I’ll gladly point you in the right direction.

Discover how this Nuclear Engineer Mastered Excel VBA

And why you should, too


Your time is valuable. It's time to become a VBA expert.

About Ryan Wells


Ryan Wells

Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA applications, including PDF VBA - a leading Excel Add-in for exporting Excel Objects, like charts and tables, to PDFs. Discover more of his popular Excel Add-ins, including Mouse To Macro and CF Shapes, at his dedicated Excel Add-ins page.