MEUserII
Board Regular
- Joined
- Oct 27, 2017
- Messages
- 91
- Office Version
- 365
- 2021
- 2019
- 2016
- 2013
- Platform
- Windows
In Excel 2016 I am trying to use the "move and click mouse" macro listed in the below link that simulates mouse click actions in Excel via VBA.
Link: https://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/
However, after pasting this code in to VBA the first two lines of code are highlighted in red font with an error window popping up with the following message:
Error Window:
Code lines highlighted in red:
For reference, the complete macro code is listed below:
For reference, I am running Excel 2016 on Windows 10 (64-bit).
How would I correct the above code, so that it works?
Link: https://excelhelphq.com/how-to-move-and-click-the-mouse-in-vba/
However, after pasting this code in to VBA the first two lines of code are highlighted in red font with an error window popping up with the following message:
Error Window:
Microsoft Vidual Basic Applications
Compile error:
The code in this project must be updated to use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute.
Code lines highlighted in red:
Code:
Public Declare Function SetCursorPos Lib "user32.dll" (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)
For reference, the complete macro code is listed below:
Code:
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
Private Sub SingleClick()
SetCursorPos 100, 100 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub
Private Sub DoubleClick()
'Double click as a quick series of two clicks
SetCursorPos 100, 100 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub
Private Sub RightClick()
'Right click
SetCursorPos 200, 200 'x and y position
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub
For reference, I am running Excel 2016 on Windows 10 (64-bit).
How would I correct the above code, so that it works?