I am trying to attach a hammer image onto the mouse cursor on Excel. It works fine without the Do Loop, that is it moves the image close to the mouse cursor when I run the macro using a button on sheet1. The problem starts when I try to make the image follow the cursor on-the-fly, using a Do Loop. In the past, I was able to make that happen without any errors, but I did not save that Excel workbook. Now, I am trying to do it again and it crashes Excel every time. I might get lucky and get a "Left method of object failed" or a "Top method of object failed" error, but that does not really help. Here is the VBA code:
Option Explicit
Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Type POINTAPI
x As Long
y As Long
End Type
Sub MoveHammer()
Dim lngStatus As Long
Dim typWhere As POINTAPI
Dim activate As Boolean
activate = True
Do While activate = True
lngStatus = GetCursorPos(typWhere)
Sheet1.Shapes("hammer").Left = 0.75 * (typWhere.x - 77)
Sheet1.Shapes("hammer").Top = 0.75 * (typWhere.y - 274)
Loop
End Sub
"hammer" is the name of the picture object that I have inserted. Thanks.
SetCursor(). You seem to be aware of the WinAPI, because you're using it forGetCursorPos().SetCursorPos(). I mentionedSetCursor(). They don't do the same things. :-)