The problem of converting VBA code to vb.net code

The problem of converting VBA code to vb.net code

lzs013
Advocate Advocate
609 Views
2 Replies
Message 1 of 3

The problem of converting VBA code to vb.net code

lzs013
Advocate
Advocate

The problem of converting VBA code to vb.net code
The following code works fine in VBA, but I tried to convert it to vb.net code but always reported an error. Who can help me to convert it?

 

VBA code:

Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare PtrSafe 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)
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Sub XZZ()
Dim P As POINTAPI
GetCursorPos P
SetCursorPos 273, 251
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
'SetCursorPos P.X, P.Y
End Sub

 

vb.net code:
Private Declare PtrSafe Function SetCursorPos Lib "user32.dll" (ByVal X As Integer, ByVal Y As Integer) As Integer
Private Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Integer
Private Structure POINTAPI
Dim X As Integer
Dim Y As Integer
End Structure

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Module xzz_Module
Public Sub xzz()
Dim P As POINTAPI
GetCursorPos(P)
SetCursorPos(273, 251)
mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
'SetCursorPos(P.X, P.Y)
End Sub

End Module

0 Likes
610 Views
2 Replies
Replies (2)
Message 2 of 3

clutsa
Collaborator
Collaborator

Can you tell use what the error is?

My first guess without really looking closely at the code is do you have all the right references set? That's the first thing I have to set when I try to convert from VBA to .Net. 

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 3 of 3

Frederick_Law
Mentor
Mentor

Did you start vb.net with VB template in Visual Studio?

There are a lot more code required in VB.net then VBA.

0 Likes