• Formatting only the nested tables

    Author
    Topic
    #508247

    Hi,
    I have written following script to find and format nested tables.

    Dim c As Integer
    Dim r As Integer
    Dim nt As Integer
    Dim TotalTables As Integer
    Dim TotalNestedTables As Integer
    Dim numColums As Integer
    Dim numRows As Integer
    TotalTables = ActiveDocument.Tables.Count
    For t = 1 To TotalTables
    Set objTable = ActiveDocument.Tables(t)
    numColums = objTable.Columns.Count
    numRows = objTable.Rows.Count
    For c = 1 To numColums
    For r = 1 To numRows
    TotalNestedTables = objTable.Cell(r, c).Tables.Count
    If TotalNestedTables > 0 Then
    For nt = 1 To objTable.Cell(r, c).Tables.Count
    objTableNested.AutoFitBehavior wdAutoFitContent
    Next nt
    End If
    Next r
    Next c
    Next t
    End Sub

    the script fails at: TotalNestedTables = objTable.Cell(r, c).Tables.Count
    and produces Run-time error 5941: The requested member of the collection does not exist.

    I can understand the error because the all tables don’t contain any nested table.

    My question is how to avoid this error?

    Viewing 2 reply threads
    Author
    Replies
    • #1591753
    • #1591782

      I’m a straight VB guy and don’t mess around with VBA very often, but when you reference a row or column isn’t its index usually 0 based?

      This is what I usually have to do to loop through columns and rows.
      For c = 0 To numColums -1
      For r = 0 To numRows – 1

    • #1591844

      Thanks Bender and BruceR
      The problem was that in some rows, few columns were merged and hence the script could not find Cell(r, c).

      What worked is that dont calculate rows and columns of the but after going in a row, calculate number of cells in that row and then loop through.

    Viewing 2 reply threads
    Reply To: Formatting only the nested tables

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

    Your information: