Recursive procedure for nested tables

T

TimvG

Suppose you have nested tables, with the nesting going arbitrarily
deep. You want to do something to every table. It seems that it
would be sensible to use a recursive procedure.

Does anyone have/know of any examples of recursive procedures for
working with nested tables?

Or is there any reason this can't be done?
 
T

TimvG

Well, let me be first to reply... It can certainly be done. I can
share some example code if there's interest.
 
G

Greg Maxey

Tim,

This is what I use. The example adds a red border to a specific cell in
each main and nested table:

Sub FormatTables()
Dim oTbl As Table
Dim oCell As Word.Cell
For Each oTbl In ActiveDocument.Tables
ProcessTable oTbl 'Process main table
'Call recursive function to process nested tables
RecursiveLoop oTbl
Next
End Sub

Function RecursiveLoop(ByRef oTblParent As Word.Table, Optional oTblChild As
Word.Table)
Dim oCellNested As Word.Cell
Dim oTblNested As Word.Table
Dim i As Long
For Each oCellNested In oTblParent.Range.Cells
If oCellNested.Tables.Count > 0 Then
For i = 1 To oCellNested.Tables.Count
Set oTblNested = oCellNested.Tables(i)
Set oTblParent = oTblNested
ProcessTable oTblNested
RecursiveLoop oTblNested, oTblParent
Next i
End If
Next oCellNested
End Function

Sub ProcessTable(oTblSingle As Table)
oTblSingle.Cell(1, 3).Borders.OutsideColorIndex = wdRed
End Sub


Well, let me be first to reply... It can certainly be done. I can
share some example code if there's interest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top