SetCursorPos Compile Error

gruntingmonkey

Active Member
Joined
Mar 6, 2008
Messages
434
Office Version
  1. 365
Platform
  1. Windows
Hello Excelheads, I have some old code that I used to use many years ago. I have just loaded on my new work computer and I am getting a compile error:

"Compile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute."

VBA Code:
Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
Sub Command2_Click()
End Sub

''''note this is an endless loop and can only be closed by pressing the ESC key
 MsgBox ("WARNING: This macro will cause an infinite loop. Press CTRL+BREAK to exit an infinite loop and then end the debugger")
Do
SetCursorPos 50, 50 '//// sets x & y position of mouse ( in pixels )
Application.Wait Now + TimeValue("00:00:05")

SetCursorPos 500, 500
Loop
End Sub

I have done some looking but don't understand the answers. I think I have to change the first line to something like:

VBA Code:
Declare Function SetCursorPos Lib "user32.dll" (ByVal x As LongPtr, ByVal y As LongPtr) As LongPtr

Can anyone point out where I am wrong?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,

Please do change as below:

VBA Code:
Declare PtrSafe Function SetCursorPos Lib "user32.dll" (ByVal x As LongPtr, ByVal y As LongPtr) As LongPtr

Thanks,
Saurabh
 
Upvote 0
So that the declarations work on 32-bit and 64-bit Windows Office:
VBA Code:
#If VBA7 Then
    Declare PtrSafe Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
#Else
    Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
#End If
PS The x and y arguments and return value aren't addresses so using LongPtr is wrong.
 
Last edited:
Upvote 0
Solution
Fantastic, both answers work! I am confused why it matters about having long vs longPtr though. Can you explain in a little more depth if at all possible?
 
Upvote 0
There should not be any LongPtr variables in there. Nothing in there is a pointer or handle.
 
Upvote 0
I meant Office, not Windows and have corrected my post.

Fantastic, both answers work! I am confused why it matters about having long vs longPtr though. Can you explain in a little more depth if at all possible?
You must use data types which match the API's signature. LongPtr is an alias which resolves to Long (32 bits) in 32-bit Office, or LongLong (64 bits) in 64-bit Office.

LongPtr, LongLong and PtrSafe are defined only in the VBA version 7 language, which comes with Office 2010 and later, hence the use of #If VBA7 Then so that these keywords are seen only when compiling on a VBA7 compiler and are ignored otherwise.

The code posted by Saurabhj won't compile on Office 2007 (VBA version 6) and earlier because it doesn't recognise LongPtr and PtrSafe. The declaration is correct on Office 2010+ 32-bit, because LongPtr resolves to Long (the correct data type for the x and y arguments and return value). On Office 2010+ 64-bit, LongPtr resolves to LongLong which would compile, but likely result in a run-time error.

More details here:
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top