Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Determing screen X Y coords from current Word text cursor position

3,744 views
Skip to first unread message

Jonathan West

unread,
Dec 21, 2005, 4:15:56 AM12/21/05
to
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


"ewo...@gmail.com" <ewolf72...@discussions.microsoft.com> wrote in
message news:26263208-DCBD-41BA...@microsoft.com...
> 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.

Helmut Weber

unread,
Dec 21, 2005, 4:56:42 AM12/21/05
to
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/microsoft.public.vb.winapi/browse_thread/thread/8bedeb6c5d9bf90f/211ec7634360de57?lnk=st&q=getcaretpos+group%3Amicrosoft.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

Helmut Weber

unread,
Dec 21, 2005, 5:03:23 AM12/21/05
to
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 & ")"

ewolf72...@discussions.microsoft.com

unread,
Dec 21, 2005, 8:44:02 PM12/21/05
to
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!

ewolf72...@discussions.microsoft.com

unread,
Dec 21, 2005, 11:32:02 PM12/21/05
to
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.

0 new messages