simulate left mouse click every 60 seconds

keith0528

Active Member
Joined
Apr 23, 2009
Messages
250
Greetings VBA guru's,

I'm trying to keep my computer active when i go to lunch. Can vba simulate a click event every 30 or 60 seconds?


thanks in advance,

K
 
Doesn't
Code:
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=Now + TimeValue("00:00:10") _
 Procedure:="KeepWindowsActive", Schedule:=False
End Sub

stop the function
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Michael,

If that was at me, I do not believe so, as the scheduled event must be referred to (including the time it was set to run) to cancel/kill.

Mark
 
Upvote 0
No, sorry Mark, you managed to get in just before me....:biggrin:
 
Upvote 0
Re: simulate left mouse click every 10 seconds

GTO, Michael,

Thank you guys for submitting your postings. Sorry for the delayed response. I've tried to incorporate both the StopTimer and StopIt sub routines but neither of them work.

StopTimer has no effect:

Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=Now + TimeValue("00:00:10"), Procedure:="KeepWindowsActive", Schedule:=False
End Sub

and StopIt debugs on me:

Sub StopIt()
Application.OnTime dtmNextTime, "Recycle", , Schedule:=False
End Sub

I don't know if it has any bearing but I had to update the declare stmts with PtrSafe because it's a 64-bit OS.


My working code:

Public Declare PtrSafe Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
Public Declare PtrSafe Sub mouse_event Lib "user32.dll" (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
Dim TimerActive As Boolean
Dim dtmNextTime As Date

Sub KeepWindowsActive()
TimerActive = True
'move cursor and click
SetCursorPos 200, 200 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Application.OnTime Now + TimeValue("00:00:05"), "KeepWindowsActive"
End Sub


Does anyone know the correct syntax to stop the timer?

thanks,
Keith
 
Upvote 0
Re: simulate left mouse click every 10 seconds

This is the only way I know how to stop it !
Code:
Sub KeepWindowsActive()
runwhen = Now + TimeSerial(0, 0, 5)
'move cursor and click
SetCursorPos 200, 200 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Application.OnTime runwhen, "KeepWindowsActive"
End Sub


Sub StopKeepWindowsActive()
Application.OnTime runwhen, "KeepWindowsActive", , False
Exit Sub
 
Upvote 0
Re: simulate left mouse click every 10 seconds

Could you post all the appropriate code, including module level variables? You can use the code tags to display it properly.

[code=rich] '...your code here[/code]
 
Upvote 0
This works great. Create buttons to call SleeperFunk and StopAllMacro:

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)


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


Public Sub SingleClick()

If ThisWorkbook.Sheets("Links").Range("V5") = "MouseClick Run" Then
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Call SleeperFunk
Else
End
End If

End Sub


Public Sub SleeperFunk()


ThisWorkbook.Sheets("Links").Range("V5") = "MouseClick Run"


Application.OnTime Now + TimeValue("00:00:10"), "SingleClick"


'Sleep (5000)


End Sub


Public Sub StopAllMacro()


'change reference cell, to stop the mouse click
ThisWorkbook.Sheets("Links").Range("V5") = "Stop AutoClick"


End Sub
 
Upvote 0

Forum statistics

Threads
1,222,097
Messages
6,163,920
Members
451,865
Latest member
dunworthc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top