
09-13-2025, 05:13 AM
|
Competent Performer
|
|
Join Date: Aug 2025
Posts: 120
|
|
Quote:
Originally Posted by Guessed
Sounds like a job for a Recursive function
Code:
Sub Demo()
Dim Tbl As Table
Debug.Print "Top Level Tables: " & ActiveDocument.Tables.Count 'this returns count of top level tables
For Each Tbl In ActiveDocument.Tables
Debug.Print "Top Table: " & Tbl.ID, Tbl.NestingLevel, Tbl.Cell(1, 1).Range.Paragraphs(1).Range.Text
If Tbl.Tables.Count > 0 Then InnerSpace Tbl
Next
End Sub
Function InnerSpace(aTbl As Table) As Table
Dim aTbl2 As Table
Debug.Print "Parent Table: " & aTbl.ID, aTbl.NestingLevel, aTbl.Cell(1, 1).Range.Paragraphs(1).Range.Text
For Each aTbl2 In aTbl.Tables
Debug.Print "Inner Table: " & aTbl2.ID, aTbl2.NestingLevel, aTbl2.Cell(1, 1).Range.Paragraphs(1).Range.Text
InnerSpace aTbl2
Next aTbl2
End Function
|
On
your code augmented:
Quote:
Sub DemoG()
Dim Tbl As Table
Debug.Print "Top Level Tables: count " & ActiveDocument.Tables.count 'this returns count of top level tables
For Each Tbl In ActiveDocument.Tables
Debug.Print "Top Table: ID " & Tbl.ID, "Level " & Tbl.NestingLevel, "Text 1,1 " & Tbl.cell(1, 1).Range.Paragraphs(1).Range.Text
If Tbl.Tables.count > 0 Then InnerSpace Tbl
Next
End Sub
Function InnerSpace(aTbl As Table) As Table
Dim aTbl2 As Table
Debug.Print "Parent Table: ID " & aTbl.ID, "Level " & aTbl.NestingLevel, " Text 1,1 " & aTbl.cell(1, 1).Range.Paragraphs(1).Range.Text
For Each aTbl2 In aTbl.Tables
Debug.Print "Inner Table: ID " & aTbl2.ID, "Level " & aTbl2.NestingLevel, " Text 1,1 " & aTbl2.cell(1, 1).Range.Paragraphs(1).Range.Text
InnerSpace aTbl2
Next aTbl2
End Function
|
gives:
Code:
Top Level Tables: count 1
Top Table: ID Level 1 Text 1,1 a
Parent Table: ID Level 1 Text 1,1 a
Inner Table: ID Level 2 Text 1,1 a
Parent Table: ID Level 2 Text 1,1 a
That (e.g. line #1) doesn't look quite as I expected
|