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.
if EWolf doesn't want to wait til Karl is back,
he could google for "getcaretposition" in "microsoft.public*".
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
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 & ")"
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!
. . .
. . .
//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.