Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Obatining cursor position (Word 2000)

    Is there a method to obtain the left and top position of the selection object. I.E. is there some method to obatin the cursor screen position. I am not referring to database cursor but the screen cusror position while in Word.

  2. #2
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obatining cursor position (Word 2000)

    Here is the answer to my posting:

    LeftPos = m_WordApp.Selection.Information(wdHorizontalPositi onRelativeToPage)
    VertPos = m_WordApp.Selection.Information(wdVerticalPosition RelativeToPage)

  3. #3
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obatining cursor position (Word 2000)

    Jim--

    It kind of depends what you're trying to do. The code you show below will give you the position (in points, where 1 pt = 1/72 inch) from the boundaries of the Word editing window. However, if you really want the position relative to the screen, using the Window.GetPoint method. Note, however, that the GetPoint method will return the position in terms of pixels. The "size" of a pixel depends on the screen resolution; you can use the GetDevCaps API function to get inches per pixel if you need it. Below is some code which illustrates the differences between return values of the Information property and the GetPoint method...Dan


    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hDc As Long, ByVal nIndex As Long) As Long

    Private Const LOGPIXELSX = 88 ' Pixel/inch in X
    Private Const LOGPIXELSY = 90 ' Pixel/inch in Y

    Public Sub ShowCurrentSelectionPos()
    Dim LeftPos As Long
    Dim VertPos As Long
    Dim lLeft As Long
    Dim lTop As Long
    Dim lWidth As Long
    Dim lHeight As Long
    Dim lVertPPI As Long
    Dim lHorzPPI As Long

    LeftPos = Selection.Information(wdHorizontalPositionRelative ToPage)
    VertPos = Selection.Information(wdVerticalPositionRelativeTo Page)
    Debug.Print "Using Information Function: LeftPos = " & LeftPos & " (" & LeftPos / 72 & " inches) . VertPos = " & VertPos & " (" & VertPos / 72 & " inches)"


    ActiveDocument.ActiveWindow.GetPoint lLeft, lTop, lWidth, lHeight, Selection.Range
    GetPixelsPerInch lVertPPI, lHorzPPI
    Debug.Print "Using GetPoint: x = " & lLeft & " (" & lLeft / lHorzPPI & " inches) . y = " & lTop & " (" & lTop / lVertPPI & " inches)"

    End Sub
    Private Sub GetPixelsPerInch(lVerticalPixelsPerInch As Long, lHorizontalPixelsPerInch As Long)
    Dim hDc As Long

    ' Get the device context of the primary monitor
    hDc = GetWindowDC(0)

    ' Get the pixels per inch on the screen
    lHorizontalPixelsPerInch = GetDeviceCaps(hDc, LOGPIXELSX)
    lVerticalPixelsPerInch = GetDeviceCaps(hDc, LOGPIXELSY)

    Debug.Print "Vertical PPI: " & lVerticalPixelsPerInch & ". Horizontal PPI: " & lHorizontalPixelsPerInch

    End Sub

  4. #4
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obatining cursor position (Word 2000)

    A slight revision to the note below. Selection.Information(wdVerticalPositionRelativeTo Page) does NOT return the distance from the top of the Word editing window, if you consider that window to be only what's visible on the screen. It appears that, instead, it returns the distance from the top of Word's logical view of the current page. You can see this illustrated by doing the following:

    1. Put the code from my previous message into a Word doc module
    2. Put some text into that document which covers more than the visible window
    3. Select some text near the bottom and run ShowCurrentSelectionPos
    4. Scroll the window up a little, so that the top of the page is no longer visible.
    5. Run ShowCurrentSelectionPos again.

    You'll see that the VerticalPositionRelativeToPage will remain constant. In other words, it's calculated based on the top of the "logical" Word page, rather than the "real" editing window visible on the screen.

    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •