Determing screen X Y coords from current Word text cursor position

E

ewolf72

Is there a way in VBA (or even VSTO, for that matter) to determine the screen
X and Y coordinates of the current text cursor position in MS Word? Even if
the coordinates are relative to the MS Word application window, I'd like to
be able to place my own custom dialog close to the text cursor when the user
invokes a key sequence to launch my code.

Any ideas? Thanks in advance.
 
J

Jonathan West

VBA provides no direct support for this. The nearest equivalent is
Selection.Information(wdHorizontalPositionRelativeToPage) and
Selection.Information(wdVerticalPositionRelativeToPage) but these give the
cursor position relative to the printed page, not to the window.

To get the cursor position relative to the window will require some Windows
API hackery. The person most likely to be able to help with this in VBA is
Karl Peterson, but I think he's offline until the new year.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
H

Helmut Weber

Hi Jonathan,
(for technical reasons I can't answer the original poster)

if EWolf doesn't want to wait til Karl is back,
he could google for "getcaretposition" in "microsoft.public*".

What I found here:
http://groups.google.de/group/micro...rosoft.public.*&rnum=3&hl=de#211ec7634360de57

enables me to get the position of the cursor,
unfortunately only in the VBA editor window!!! :-(

But maybe it gets the OP going.

Public Declare Function GetCaretPos Lib "user32" _
(lpPoint As POINTAPI) As Long
Private Type POINTAPI ' 8 Bytes
x As Long
y As Long
End Type

Public Sub WhereAmI()
Dim p As POINTAPI
Dim l As Long
l = GetCaretPos(p)
If l <> 0 Then
Debug.Print "(" & p.x & "," & p.y & ")"
End If
'MyStart
End Sub

Sub MyStart()
Application.OnTime _
When:=Now + TimeValue("00:00:03"), _
Name:="WhereamI"
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
H

Helmut Weber

Got it!

Use "getcursorpos" instaed of "getcaretpos":


Public Declare Function GetCaretPos Lib "user32" _
(lpPoint As POINTAPI) As Long
Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long

Private Type POINTAPI ' 8 Bytes
x As Long
y As Long
End Type

Public Sub WhereAmI()
Dim p As POINTAPI
Dim l As Long
l = GetCursorPos(p)
If l <> 0 Then
StatusBar = "(" & p.x & "," & p.y & ")"
End If
MyStart
End Sub

Sub MyStart()
Application.OnTime _
When:=Now + TimeValue("00:00:03"), _
Name:="WhereamI"

End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
E

ewolf72

Helmut -

I'm sorry if I wasn't clear in my original post but I am interested in
determining the X and Y coordinates of the -text- cursor (or "caret"). The
code you provided returns the X,Y coordinates of the mouse cursor. Is there
a way to return the X,Y coords for the -text- cursor? Thanks!
 
E

ewolf72

Actually, a colleague helped me by providing some sample code for me to work
with. Here is what I ended up with (C#):

. . .
. . .
//position dialog relative to word insertion point (caret)
int left = 0;
int top = 0;
int width = 0;
int height = 0;
MSWord.Range r = Globals.ThisDocument.Application.Selection.Range;
MSWord.Window w = Globals.ThisDocument.ActiveWindow;

w.GetPoint( out left, out top, out width, out height, r );

frmPopUp newForm = new frmPopUp();
newForm.SetDesktopLocation( left + width + 2, top -
newForm.Height + height );
. . .
. . .

Hope this helps someone else, too.
 

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