Introduction | Example | Tutorial | Applications

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 your recording 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

Make powerful macros with our free VBA Developer Kit

This is actually pretty neat. If you have trouble understanding or remembering it, our free VBA Developer Kit can help. It’s loaded with VBA shortcuts to help you make your own macros like this one - we’ll send a copy, along with our Big Book of Excel VBA Macros, to your email address below.

I'll take a free VBA Developer Kit

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.

That’s all for this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form below.