peterv
peterv

Reputation: 167

accessing nested tables in MS Word by VBA

I have a Word document that contains 300+ tables that each have only one row and two columns. In the first column of every of one of these tables, there is some text + another table. I need to change the style of these nested tables (the style of the other tables should not change), but I am not able to select them.

I tried the following:

Dim mytable As Table
Dim nestedtable As Table

For Each mytable In ActiveDocument.Tables
    For Each nestedtable In mytable
        nestedtable.Select
    Next
Next

I also tried:

Dim mytable As Table
Dim nestedtable As Table

For Each mytable In ActiveDocument.Tables
    mytable.Select
    For Each nestedtable In Selection
        nestedtable.Select
    Next
Next

How could I select these nested tables?

Upvotes: 0

Views: 3703

Answers (2)

Jean-Pierre Oosthuizen
Jean-Pierre Oosthuizen

Reputation: 2683

I have not included the Select part as this is to be avoided at all costs as you will figure out as you continue programming with VBA.

Also try give more descriptive names to the variables you declare, this way you train your brain for later when you might forget the multiple variables you declare.

Sub ModifyNestedTables()

    Dim DocumentBodyTable As Table
    Dim NestedTable As Table

    For Each DocumentBodyTable In ActiveDocument.Tables
        For Each NestedTable In DocumentBodyTable.Tables

            With NestedTable
                .ApplyStyleRowBands = False
                .ApplyStyleHeadingRows = False
                .ApplyStyleFirstColumn = False
            End With

        Next NestedTable
    Next DocumentBodyTable

End Sub

Upvotes: 2

peterv
peterv

Reputation: 167

OK, this is embarrassing, I found the answer to my own question after 10 more minutes of searching. Anyway, for anyone who has the same problem in the future, this is how you can access the nested tables (and change the style):

Dim mytable As Table
Dim nestedtable As Table
For Each mytable In ActiveDocument.Tables
    For Each nestedtable In mytable.Tables
        nestedtable.Select
        Selection.Tables(1).ApplyStyleRowBands = False
        Selection.Tables(1).ApplyStyleHeadingRows = False
        Selection.Tables(1).ApplyStyleFirstColumn = False
    Next
Next

Upvotes: 0

Related Questions