I'm writing a set of macros to format a series of tables in a document. Part of this requires inserting a new row at the end of each table - however, when I try to do it using For Each <table> in ActiveDocument.Tables, it ends up inserting an infinite number of rows at the bottom of the first table.
Both of these subs will enter an infinite loop:
Sub insertBottomRow1()
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add
'Other row formatting
Next theTable
End Sub
Sub insertBottomRow2()
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
theTable.Rows.Last.Select
Selection.InsertRowsBelow
Set theNewRow = theTable.Rows.Last
'Other row formatting
Next theTable
End Sub
I have a similar sub which uses the same structure to insert a row at the top of the tables, and this does not enter an infinite loop.
Sub insertTopRow()
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add(theTable.Rows.First)
'Other row formatting
Next theTable
End Sub
I've done some experimentation (adding counters to the added rows, and stepping through the debugger), and the two subs that crash seem to be re-selecting the first table instead of moving onto the next table.
Any help would be appreciated. Thank you.