none
failed to read a table's width in word? RRS feed

  • Question

  • sub width()

    dim tw

    tw = activedocument.tables(1).width

    end sub

    i failed to read the 1st table's width in word??

    help!!!!

    Thursday, August 16, 2012 4:40 PM

Answers

  • You could use code like:

    Sub GetTableWidths()
    Application.ScreenUpdating = False
    Dim oTableWidth As Single, oTbl As Table, oCel As Cell, sVPos As Single
    ' Test all tables
    For Each oTbl In ActiveDocument.Tables
      sVPos = oTbl.Range.Information(wdVerticalPositionRelativeToPage)
      ' Calculate the current table's width as the sum of the cell widths
      ' in the first row. The approach taken is immune to merged cells.
      oTableWidth = 0
      For Each oCel In oTbl.Range.Cells
        ' Confirm we're still on the first row
        If oCel.Range.Information(wdVerticalPositionRelativeToPage) = sVPos Then
          ' Accumulate the cell widths
          oTableWidth = oTableWidth + oCel.Width
        Else
          Exit For
        End If
      Next oCel
      ' Output the result of our deliberations
      MsgBox "The current table width is " & PointsToCentimeters(oTableWidth) & "cm"
    Next oTbl
    Application.ScreenUpdating = True
    End Sub

    Note that the PreferredWidth property won't necessarily return meaningful results - and even where it does you'd need to retrieve it's type also as it could be expressed as a %.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, August 17, 2012 6:47 AM

All replies

  • There's the PreferredWidth property.

    Thursday, August 16, 2012 5:31 PM
  • You could use code like:

    Sub GetTableWidths()
    Application.ScreenUpdating = False
    Dim oTableWidth As Single, oTbl As Table, oCel As Cell, sVPos As Single
    ' Test all tables
    For Each oTbl In ActiveDocument.Tables
      sVPos = oTbl.Range.Information(wdVerticalPositionRelativeToPage)
      ' Calculate the current table's width as the sum of the cell widths
      ' in the first row. The approach taken is immune to merged cells.
      oTableWidth = 0
      For Each oCel In oTbl.Range.Cells
        ' Confirm we're still on the first row
        If oCel.Range.Information(wdVerticalPositionRelativeToPage) = sVPos Then
          ' Accumulate the cell widths
          oTableWidth = oTableWidth + oCel.Width
        Else
          Exit For
        End If
      Next oCel
      ' Output the result of our deliberations
      MsgBox "The current table width is " & PointsToCentimeters(oTableWidth) & "cm"
    Next oTbl
    Application.ScreenUpdating = True
    End Sub

    Note that the PreferredWidth property won't necessarily return meaningful results - and even where it does you'd need to retrieve it's type also as it could be expressed as a %.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, August 17, 2012 6:47 AM