Automation for cont. screen movement

timer with mouse movements

Certainly! Below is the complete VBA code in a single module that will move the mouse every 10 seconds using Windows API functions, along with the other Excel tasks (like updating a cell).

Full VBA Code (One Module)

  1. Open Excel and press Alt + F11 to open the VBA editor.

  2. In the VBA editor, Insert a new module:

    • Click on Insert > Module.
  3. Paste the following code into the module:

' Declare the Windows API function to move the mouse cursor
#If VBA7 Then
    Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
#Else
    Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
#End If

Dim nextRun As Date

Sub StartTimer()
    ' Set the timer to run every 10 seconds
    nextRun = Now + TimeValue("00:00:10")
    Application.OnTime nextRun, "RunTask"
End Sub

Sub RunTask()
    ' Move the mouse to random coordinates on the screen
    Dim x As Long, y As Long
    x = Int((1920 - 1 + 1) * Rnd + 1)  ' Random x value between 1 and 1920 (for Full HD screen)
    y = Int((1080 - 1 + 1) * Rnd + 1)  ' Random y value between 1 and 1080 (for Full HD screen)

    ' Call the API to move the mouse to random coordinates
    MoveMouse x, y

    ' Optionally, perform other Excel tasks here (e.g., updating a cell)
    Sheets("Sheet1").Range("A1").Value = "Mouse moved at " & Now

    ' Reschedule the next run
    StartTimer
End Sub

Sub MoveMouse(x As Long, y As Long)
    ' Call the Windows API function to move the mouse to (x, y)
    SetCursorPos x, y
End Sub

Sub StopTimer()
    ' Stop the timer to prevent the task from running again
    On Error Resume Next
    Application.OnTime nextRun, "RunTask", , False
End Sub

How it Works:

  • Declare Windows API: We use the SetCursorPos function from the user32.dll library to move the mouse cursor.

  • StartTimer: This subroutine sets the timer to trigger the RunTask subroutine every 10 seconds using Application.OnTime.

  • RunTask: This subroutine is called every 10 seconds. It moves the mouse to random coordinates (between 1 and 1920 for x, and between 1 and 1080 for y), updates cell A1 on Sheet1, and then reschedules itself.

  • MoveMouse: This subroutine calls the Windows API function to actually move the mouse cursor to the specified coordinates.

  • StopTimer: This subroutine stops the timer from triggering the task again.

How to Use:

  1. Run StartTimer:

    • Press Alt + F8, select StartTimer, and click Run. This will start the automation and move the mouse every 10 seconds.
  2. Stop the Timer:

    • To stop the timer, you can run the StopTimer subroutine.

Notes:

  • Random Coordinates: The mouse will move to random coordinates on a Full HD screen (1920x1080). You can adjust the screen resolution range based on your screen's resolution.

  • Excel Task: The code also updates cell A1 on Sheet1 with the current time to show that the task is running.

  • If you want to change the mouse behavior (e.g., move it to specific coordinates), you can modify the RunTask subroutine to set x and y to fixed values.

Saving the Workbook:

  • Save the workbook as a macro-enabled file (.xlsm) to ensure the VBA code is preserved.

This should give you the automation of mouse movement every 10 seconds along with an Excel task. Let me know if you have further questions or adjustments!