Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I'm getting some complex tables, ones with merged rows and columns, and occassionally tables within tables. I need to determine the position of the right edge, or at least the width of the table. How can I do this?

    There is no single Right position, and adding up the indvidual cells' width may not work if they're merged (showing a width of 9999999).

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi ForEachLoop,

    Try something along the lines of:
    Code:
    Sub Demo()
    Dim tRng As Range, sngWdth As Single
    With ActiveDocument.Tables(1)
    Set tRng = .Rows(1).Range
    tRng.MoveEnd wdCharacter, -1
    sngWdth = -tRng.Information(wdHorizontalPositionRelativeToPage)
    tRng.Start = tRng.End
    sngWdth = sngWdth + tRng.Information(wdHorizontalPositionRelativeToPage)
    MsgBox sngWdth
    End With
    End Sub
    The result is returned in points.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    That's an excellent answer, an approach I had not thought of, if it were a simple table. However, these tables have merged rows and columns, so that code can't be used. For the time being I tried something like this, adding up cell widths. Ugly and slow. Comments?

    Code:
          Dim rightmostPosition As Long
            Dim rowWidth As Long
            Dim myCell As Cell
            Dim cellCount As Long
            Dim i As Long
            For i = 1 To ActiveDocument.Tables(1).Rows.Count
            
                cellCount = 1
                rowWidth = 0
                
                For Each myCell In ActiveDocument.Tables(1).Range.Cells
                    If ActiveDocument.Tables(1).Range.Cells(cellCount).RowIndex = i Then
                        'myCell.Select
                        rowWidth = rowWidth + ActiveDocument.Tables(1).Range.Cells(cellCount).Width
                    End If
                    cellCount = cellCount + 1
                    
                    DoEvents
                Next
                
                If rowWidth < 9999999 Then
                    If rowWidth > rightmostPosition Then rightmostPosition = rowWidth
                End If
            
                DoEvents
            Next i

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi ForEachLoop,

    OK. Try this:
    Code:
    Sub Demo()
    Dim tRng As Range, sngWdth As Single
    With ActiveDocument.Tables(1)
    Set tRng = .Cell(1, 1).Range
    sngWdth = -tRng.Information(wdHorizontalPositionRelativeToPage)
    Do While tRng.Cells(1).RowIndex = 1
      tRng.Move unit:=wdCell, Count:=1
    Loop
    tRng.MoveEnd wdCharacter, -1
    sngWdth = sngWdth + tRng.Information(wdHorizontalPositionRelativeToPage)
    MsgBox sngWdth
    End With
    End Sub
    Unless you have rows of different widths, this should do the job.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Looking at it from a completely lateral direction - I have this problem, and to fix it for me, I draw a box over the table, matching the edges to the table's lines - and the size of the box is the size of the table ... probably not as precise as some of the VBA answers above, but it does the job for my purposes (getting a general size to match the size of an XL chart to, before copying it over).
    Beryl M


  6. #6
    Star Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    54
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Macropod, thanks for the code. Sorry for the delay. This did the trick and works well.

Posting Permissions

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