Reputation: 11
I'm getting a compile error when running the code below. The goal of the code is to move the cursor and click on cells in the excel doc. The mouse_event seems to be causing the issue but I'm not sure why (perhaps the wrong formatting?).
The VBA editor is also highlighting the first two lines red, which I'm not sure why it's doing that.
Thank you for your help!
Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As LongPtr, ByVal y As LongPtr) As LongPtr
Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As LongPtr, ByVal dx As LongPtr, ByVal dy As LongPtr, ByVal cButtons As LongPtr, ByVal dwExtraInfo As LongPtr)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As LongPtr = &H8
Public Const MOUSEEVENTF_RIGHTUP As LongPtr = &H10
Sub MoveMousePlease()
Dim i As Integer
For i = 1 To 9999
'For Info, number of iteration
'Cells(1, 1) = i
If Cells(3, 5) = "" Then
SetCursorPos 350, 300 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
WaitPlease
SetCursorPos 350, 360 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
WaitPlease
SetCursorPos 350, 420 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
WaitPlease
SetCursorPos 350, 480 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
WaitPlease
Else
Exit For
End If
Next i
End Sub
Sub WaitPlease()
Dim sngWaitEnd As Single
sngWaitEnd = Timer + 5
Do
DoEvents
Cells(3, 3).Value = Timer
Loop Until Timer >= sngWaitEnd
End Sub
Upvotes: 1
Views: 203
Reputation: 5805
First off, don't do that. You can do everything using VBA commands without moving the mouse and simulating clicks.
If you read the documentation you will find that they say not to use mouse_event
in favor of SendInput
Now the answer:
Your code works in 64-bit Excel. This means that you are using a 32-bit host which requires a different signature.
Try this:
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Boolean
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As LongPtr, ByVal dx As LongPtr, ByVal dy As LongPtr, ByVal cButtons As LongPtr, ByVal dwExtraInfo As LongPtr)
Upvotes: 1