【发布时间】:2019-08-28 08:59:17
【问题描述】:
我正在尝试在一个循环中运行无限的鼠标左键单击,当时间达到特定值(即特定小时)时应该中断该循环。左键单击应始终以 5 秒的间隔在同一单元格中进行。我使用的是 Windows 10 和 Excel 2016。
我尝试编译一些代码,但计时器功能似乎无法正常工作,并且鼠标单击不会发生在同一个单元格中。任何建议将不胜感激。
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)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Timer()
Dim t As Date, tStop As Date
t = Now
tStop = t + TimeValue("19:43:00") 'Adjust the TimeValue as needed "hh:mm:ss"
Do Until t = tStop
DoEvents
SetCursorPos 634, 371 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Sleep 5000
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
t = Now
Loop
MsgBox "t = " & t & vbCrLf & "tStop = " & tStop
End Sub
【问题讨论】:
-
这样做的目的是什么?你知道Application.OnTime?