Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Find Left and Top Coordinates of a Table Cell

    How do you find the coordinates for the Left and Top (for that matter, the Right and Bottom) boundaries of a given table cell? The following does produce very accurate results, especially for the X:
    Public Sub FindCoordinates()
    On Error GoTo MyErrorHandler

    Dim currentDocument As Document
    Set currentDocument = ActiveDocument

    Dim oRng As Range
    Set oRng = Selection.Tables(1).Cell(3, 3).Range
    oRng.Select

    Dim x As Single
    x = oRng.Information(wdHorizontalPositionRelativeToPag e)

    Dim y As Single
    y = oRng.Information(wdVerticalPositionRelativeToPage)

    'Just to see the points
    ActiveDocument.Shapes.AddLine x, 0, x, 2000
    ActiveDocument.Shapes.AddLine 0, y, 2000, y
    Exit Sub
    MyErrorHandler:
    MsgBox "FindCoordinates" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Sub

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    It appears that you need to
    1. set the table to float to get to the right result for y
    2. include the cell widths for 3,1 and 3,2 for the x value
    Code:
    Public Sub FindCoordinates()
      Dim oRng As Range, oTbl As Table
      Dim x As Single, y As Single
      
      On Error GoTo MyErrorHandler
      Set oTbl = Selection.Tables(1)
      oTbl.Rows.WrapAroundText = True
      Set oRng = oTbl.Cell(3, 3).Range
      oRng.Select
    
      x = oRng.Information(wdHorizontalPositionRelativeToPage)
      x = x + oTbl.Cell(3, 1).Width + oTbl.Cell(3, 2).Width
      y = Selection.Information(wdVerticalPositionRelativeToPage)
    
      'Just to see the points
      ActiveDocument.Shapes.AddLine x, 0, x, 2000
      ActiveDocument.Shapes.AddLine 0, y, 2000, y
      
      'oTbl.Rows.WrapAroundText = False
      Exit Sub
    MyErrorHandler:
      MsgBox "FindCoordinates" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks! Adding the widths of the previous cells was the missing detail. It's too bad that this information isn't already in the table structure.

Posting Permissions

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