Word and WinApi - moving caret to cursor

G

Greg

Please help, how in VBA PROGRAM move the caret to the location of the cursor

the given code does not work:

Public Declare Function GetActiveWindow _
Lib "user32" () As Long

Public Declare Function GetCursorPos _
Lib "user32" _
(pp As POINT) As Boolean

Public Declare Function ScreenToClient _
Lib "user32" _
(ByVal hwnd As Long, pp As POINT) As Boolean

Public Declare Function SetCaretPos _
Lib "user32" _
(ByVal x As Integer, ByVal y As Integer) As Boolean

Public Type POINT
x As Long
y As Long
End Type
'-------------------

sub SetCur()

Dim screen As POINT, q as Boolean

'get WORD descriptor
hwnd = GetActiveWindow

'get cursor position
q = GetCursorPos(screen)

' transforming screen coordinates to client
q = ScreenToClient(hwnd, screen)


' trying to move the caret-> don't working !!!!!!!!!!!
q = SetCaretPos(screen.x, screen.y)

end sub
 
H

Helmut Weber

Hi Greg,

not that I think I understand much of all that API-stuff,
but at a long shot,
when I look into my API-Reference, GetCursorPos,
ScreentoClient, SetCaretPos are all of type "long".

I wonder ....
 
A

Andi Mayer

Please help, how in VBA PROGRAM move the caret to the location of the cursor

the given code does not work:

Public Declare Function GetActiveWindow _
Lib "user32" () As Long

Public Declare Function GetCursorPos _
Lib "user32" _
(pp As POINT) As Boolean

Public Declare Function ScreenToClient _
Lib "user32" _
(ByVal hwnd As Long, pp As POINT) As Boolean

Public Declare Function SetCaretPos _
Lib "user32" _
(ByVal x As Integer, ByVal y As Integer) As Boolean

Public Type POINT
x As Long
y As Long
End Type
'-------------------

sub SetCur()

Dim screen As POINT, q as Boolean

'get WORD descriptor
hwnd = GetActiveWindow

'get cursor position
q = GetCursorPos(screen)

' transforming screen coordinates to client
q = ScreenToClient(hwnd, screen)


' trying to move the caret-> don't working !!!!!!!!!!!
q = SetCaretPos(screen.x, screen.y)

end sub

I don't know the api SetCaretPos, if you want to set the CorsourPos
use SetCursorPos

To Helmuth: in this case there is now difference, this function only
gives a true or false as result (better done or not done)
 
A

Andi Mayer

---------------------------------
to Andi Mayer

Thanks, but I don't want to set the CURSOR position, I would like to MOVE
the CARET position (in MS WORD ) to CURSOR position

This was a Language problem on my side

do I understand you right:
you want to simulate a left-mouse-click?

if yes then use the mouse_event api

Private Declare 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)
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Form_Activate()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: (e-mail address removed)
Do
'Simulate a mouseclick on the cursor's position
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&,
0&, cButt, dwEI
DoEvents
Loop
End Sub
 
G

Greg

This was a Language problem on my side

do I understand you right:
you want to simulate a left-mouse-click?

if yes then use the mouse_event api

Private Declare 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)
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Form_Activate()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: (e-mail address removed)
Do
'Simulate a mouseclick on the cursor's position
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&,
0&, cButt, dwEI
DoEvents
Loop
End Sub


Yes, it is!
It's working with some modifing

mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP,0&, 0&, 0&,
GetMessageExtraInfo()


Sorry for my bad English.
With the best regards from Moscow

P.S. HAPPY NEW YEAR!
 
A

Andi Mayer

Sorry for my bad English.

?? your english is perfect,.

go to the website mentoned above, there is a small program called
API-Guide with a lot of explanations and samples.


P.S. HAPPY XMas in advance for your
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top