VBA Place cursor away from part window

VBA Place cursor away from part window

rcolon9E4ZX
Advocate Advocate
2,599 Views
1 Reply
Message 1 of 2

VBA Place cursor away from part window

rcolon9E4ZX
Advocate
Advocate

Hey guys,

 

I have created a program to export the bill of materials to excel, one step of which opens parts and captures the thumbnails. Sometimes, when the cursor is over the part, I get a red shaded face. The hope is to re - position the cursor away from the part window.

 

I have found and modified a code that works great with VBA within excel. I can get and set the location of the cursor.

 

 

Cursor VBA Excel.PNG

 

 

 

 

 

 

 

 

Cursor VBA Excel References.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cursor Excel.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

' Access the GetCursorPos function in user32.dll
Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long
' Access the GetCursorPos function in user32.dll
Declare Function SetCursorPos Lib "user32" _
(ByVal x As Long, ByVal y As Long) As Long

' GetCursorPos requires a variable declared as a custom data type
' that will hold two integers, one for x value and one for y value
Type POINTAPI
   X_Pos As Long
   Y_Pos As Long
End Type
Private Sub Get_Cursor_Pos()

    ' Main routine to dimension variables, retrieve cursor position,
    ' and display coordinates
    Range("A1").Value = "X"
    Range("A2").Value = "Y"
    
    Dim i As Integer
    i = 0
    
Do Until i = 10000

    ' Dimension the variable that will hold the x and y cursor positions
    Dim Hold As POINTAPI

    ' Place the cursor positions in variable Hold
    GetCursorPos Hold

    ' Display the cursor position coordinates
'      MsgBox "X Position is : " & Hold.X_Pos & Chr(10) & _
'         "Y Position is : " & Hold.Y_Pos
    
    Range("B1").Value = Hold.X_Pos
    Range("B2").Value = Hold.Y_Pos
    
    i = i + 1
         
Loop

End Sub
Private Sub Set_Cursor_Pos()

' Routine to set cursor position

' Looping routine that positions the cursor
   For x = 1 To 480 Step 20
      SetCursorPos x / 46.1, x / 46.1
      For y = 1 To 40000: Next
   Next x
End Sub

 

 

However, when I bring the same code into Inventor and try to run it, I get:

 

 

Cursor VBA Inventor.PNGCursor VBA Inventor References.PNG

Cursor VBA Inventor Error.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What must be changed so that the code may work with 64-bit Inventor 2017?

Office 2013

Windows 7 Professional 64 bit

Please let me know if any more information is required.

 

Any help with this would be much appreciated.

0 Likes
Accepted solutions (1)
2,600 Views
1 Reply
Reply (1)
Message 2 of 2

rcolon9E4ZX
Advocate
Advocate
Accepted solution

I figured out my own problem. I love when the solution is a 100 times simpler than imagined.

 

Hold your breath...

 

Declare PtrSafe Function SetCursorPos Lib "user32" _
(ByVal x As Long, ByVal y As Long) As Long

Simply adding 'PtrSafe' after Declare solves the issue and the program works in 64bit Inventor now.

 

Here is the code to re-position the cursor anywhere on the screen. Currently set to top left of screen. (10,10) [x,y]

 

' Access the GetCursorPos function in user32.dll
Declare PtrSafe Function SetCursorPos Lib "user32" _
(ByVal x As Long, ByVal y As Long) As Long
Private Sub Set_Cursor_Pos()

    SetCursorPos 10, 10

End Sub

It would be interesting to know what the PtrSafe Attribute is doing for the declaration in a 64bit application.

 

Hope this helps someone else.

0 Likes