troubleshooting Question

How to change the cursor?

Avatar of Daniel Pineault
Daniel Pineault asked on
Microsoft OfficeOutlookMicrosoft 365VBA
7 Comments1 Solution108 ViewsLast Modified:
I'm needing to change the cursor in Outlook while a proc is running.

In Office x32 I use, and it works.

Private Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
Private Declare Function SetSystemCursor Lib "user32" (ByVal hcur As Long, ByVal id As Long) As Long
Private Declare Function DestroyCursor Lib "user32" (ByVal hCursor As Long) As Long
Private Const OCR_NORMAL = 32512
Private Const IDC_WAIT = 32514&

Sub CursorWait()
    Dim hCursor               As Long

    On Error GoTo Error_Handler

    hCursor = LoadCursor(ByVal 0&, IDC_WAIT)
    SetSystemCursor hCursor, OCR_NORMAL
    DestroyCursor hCursor

Error_Handler_Exit:
    On Error Resume Next
    Exit Sub

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: CursorWait" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

Sub CursorNormal()
    Dim hCursor               As Long

    On Error GoTo Error_Handler

    hCursor = LoadCursor(ByVal 0&, OCR_NORMAL)
    SetSystemCursor hCursor, IDC_WAIT
    DestroyCursor hCursor

Error_Handler_Exit:
    On Error Resume Next
    Exit Sub

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: CursorNormal" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

However, when I try to port it over to x64, nothing.  No error, but no change in cursor either.

Public Declare PtrSafe Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As LongPtr, ByVal lpCursorName As String) As LongPtr
Private Declare PtrSafe Function SetSystemCursor Lib "user32" (ByVal HCur As LongPtr, ByVal id As Long) As Long
Private Declare PtrSafe Function DestroyCursor Lib "user32" (ByVal hCursor As LongPtr) As Long
Private Const OCR_NORMAL = 32512
Private Const IDC_WAIT = 32514&

Sub CursorWait()
    Dim hCursor               As LongPtr

    On Error GoTo Error_Handler

    hCursor = LoadCursor(ByVal 0&, IDC_WAIT)
    SetSystemCursor hCursor, OCR_NORMAL 
    DestroyCursor hCursor

Error_Handler_Exit:
    On Error Resume Next
    Exit Sub

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: CursorWait" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

Sub CursorNormal()
    Dim hCursor               As LongPtr

    On Error GoTo Error_Handler

    hCursor = LoadCursor(ByVal 0&, OCR_NORMAL)
    SetSystemCursor hCursor, IDC_WAIT
    DestroyCursor hCursor

Error_Handler_Exit:
    On Error Resume Next
    Exit Sub

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: CursorNormal" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

When checking I notice that LoadCursor is returning 0^ instead of a proper value.  What am I doing wrong, or what approach would universally work?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros