Set ws = CreateObject("WScript.Shell") '创建一个shell对象(ws.SendKeys "{Enter}"用这句的时候用,不然这句可以用单引号注释掉)
Set mouse = New SetMouse '创建一个SetMouse的实例对象mouse
for i=1 to 10 '循环执行10次,根据需要设置
WScript.sleep 1000 '1000=1秒,如果10分钟可以写成60000*10,sleep即等待多少时间
mouse.move 800,600 '调用鼠标移动过程,鼠标移动到坐标(800,600)。根据需要修改
mouse.click "DBCLICK" '调用鼠标点击过程,DBCLICK是执行左键双击,如果执行左键单击则用LEFT
'回车可以使用ws.SendKeys "{Enter}"
next '进入下一次循环,直到i=10
'下面是一个鼠标SetMouse功能类
Class SetMouse
private S
private xls, wbk, module1
private reg_key, xls_code, x, y
'初始化的子过程-不可调用
Private Sub Class_Initialize()
Set xls = CreateObject("Excel.Application")
Set S = CreateObject("wscript.Shell")
'vbs 完全控制excel
reg_key = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM"
reg_key = Replace(reg_key, "$", xls.Version)
S.RegWrite reg_key, 1, "REG_DWORD"
'model 代码
xls_code = _
"Private Type POINTAPI : X As Long : Y As Long : End Type" & vbCrLf & _
"Private Declare Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long" & vbCrLf & _
"Private Declare Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long" & vbCrLf & _
"Private Declare Sub mouse_event Lib ""user32"" Alias ""mouse_event"" " _
& "(ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)" & vbCrLf & _
"Public Function getx() As Long" & vbCrLf & _
"Dim pt As POINTAPI : GetCursorPos pt : getx = pt.X" & vbCrLf & _
"End Function" & vbCrLf & _
"Public Function gety() As Long" & vbCrLf & _
"Dim pt As POINTAPI: GetCursorPos pt : gety = pt.Y" & vbCrLf & _
"End Function"
Set wbk = xls.Workbooks.Add
Set module1 = wbk.VBProject.VBComponents.Add(1)
module1.CodeModule.AddFromString xls_code
End Sub
'关闭-不可调用过程
Private Sub Class_Terminate
xls.DisplayAlerts = False
wbk.Close
xls.Quit
End Sub
'获取鼠标当前坐标
Public Sub getpos( x, y)
x = xls.Run("getx")
y = xls.Run("gety")
End Sub
'移动鼠标至新位置
Public Sub move(x,y)
xls.Run "SetCursorPos", x, y
End Sub
'鼠标点击事件过程
Public Sub click(keydown)
Select Case UCase(keydown)
Case "LEFT" '左键单击
xls.Run "mouse_event", &H2 + &H4, 0, 0, 0, 0
Case "RIGHT" '右键单击
xls.Run "mouse_event", &H8 + &H10, 0, 0, 0, 0
Case "MIDDLE" '中键单击
xls.Run "mouse_event", &H20 + &H40, 0, 0, 0, 0
Case "DBCLICK" '左键双击
xls.Run "mouse_event", &H2 + &H4, 0, 0, 0, 0
xls.Run "mouse_event", &H2 + &H4, 0, 0, 0, 0
End Select
End Sub
End Class