• Determine Width of Table

    Author
    Topic
    #472288

    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).

    Viewing 4 reply threads
    Author
    Replies
    • #1249537

      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
      [Fmr MS MVP - Word]

    • #1249796

      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  rightmostPosition Then rightmostPosition = rowWidth
                  End If
              
                  DoEvents
              Next i
    • #1249902

      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
      [Fmr MS MVP - Word]

    • #1250996

      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).

    • #1252043

      Macropod, thanks for the code. Sorry for the delay. This did the trick and works well.

    Viewing 4 reply threads
    Reply To: Determine Width of Table

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: