Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Word VBA to find x,y cordinates of selected range of text (Start and End)  (Read 8049 times)

0 Members and 1 Guest are viewing this topic.

ddjedidd

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Windows 7
    I am looking for help with a MS Word VBA code that will run in a macro to find the x and y coordinates of selected range of text.

    xposition = Selection.Information(wdHorizontalPositionRelativeToPage)
    yposition = Selection.Information(wdVerticalPositionRelativeToPage)

    The above code provides the X and Y of the START of the selection (or where the curser is).

    How do I find the x and y of the END of the Range of Selection?
    (If it matters, I need it to run in standard text and also when the text is in a word Table)

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Code: [Select]
    Dim Left, Right, Top, Bottom
       
       Set Starting = Selection.Range
       Starting.SetRange Starting.Start, Starting.Start
       Left = Starting.Information(wdHorizontalPositionRelativeToPage)
       Top = Starting.Information(wdVerticalPositionRelativeToPage)
       
       Set Ending = Selection.Range
       Ending.SetRange Ending.End, Ending.End
       
       Right = Ending.Information(wdHorizontalPositionRelativeToPage)
       Bottom = Ending.Information(wdVerticalPositionRelativeToPage)
         
       'MessageBox.Show "Left:" + Left + " Top:" + Top + " Right:" + Right + " Bottom:" + Bottom

    Of course the "End" of the selection  could be anywhere, so this wouldn't give you a rectangle in most cases. I couldn't figure out how to get the actual width/height of the selection itself.
    I was trying to dereference Null Pointers before it was cool.

    ddjedidd

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Windows 7
      Thank you!! Thank you!!