每30分钟移动一次鼠标指针

时间:2017-06-05 12:15:44

标签: excel vba

我希望我的鼠标指针每30分钟自动移动一次 我在Excel VBA中编写代码。
我试过https://support.microsoft.com/en-us/help/152969/visual-basic-procedure-to-get-set-cursor-position,但它没有用。

3 个答案:

答案 0 :(得分:1)

使用以下代码创建新模块:

{'key': 'a', 'key_2': 'b'}

调用Private dtmNext As Date Private Type POINTAPI x As Long y As Long End Type Private Declare Function GetCursorPos Lib "user32" (Point As POINTAPI) As Long Private Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Long Sub Move_Cursor() Dim Hold As POINTAPI GetCursorPos Hold SetCursorPos Hold.x + 30, Hold.y dtmNext = DateAdd("n", 30, Now) Application.OnTime dtmNext, "Move_Cursor" End Sub Sub Stop_Cursor() Application.OnTime dtmNext, "Move_Cursor", , False End Sub 每30分钟开始移动光标。要停止自动运动,请使用

Move_Cursor()

答案 1 :(得分:0)

它确实可以工作,但是您需要创建一个模块。右键点击ThisWorksheet-->insert-->Module

只有这样,它才能起作用。

答案 2 :(得分:0)

感谢您的第一个回答,我只是做了一些更新(PtrSafe 声明)和更友好的名称

1-创建一个名为“鼠标”的模块

2-此代码:

Private Type mousePos
    x As Long
    y As Long
End Type
Private Declare PtrSafe Function GetCursorPos Lib "user32" (ByRef p As mousePos) As Long
Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Long
Private Function mouseGet(ByRef p As mousePos) As Long: GetCursorPos p: End Function
Function mouseSet(x, y As Integer): SetCursorPos IIf(x < 0, 0, x), IIf(y < 0, 0, y): End Function
Function mouseOffset(Optional x As Integer = 0, Optional y As Integer = 0)
    Dim Hold As mousePos
    mouseGet Hold
    mouseSet Hold.x + x, Hold.y + y
End Function

3-Code in another module to only test (只使用mouseSetmouseOffset,如果你需要mouseGet就去掉Private,这些只是方法的例子调用它

Sub mouseTest()
    Mouse.mouseSet 200, 200
          mouseSet 300, 300
    Mouse.mouseOffset 200    'y is optional
    Mouse.mouseOffset , 200  'x is optional
    Mouse.mouseOffset y:=200 'x is optional
          mouseOffset 200    'y is optional
          mouseOffset 200, 200
End Sub

IIf 的原因是因为负数会将鼠标设置在屏幕的另一侧