![]() |
|
#1
|
|||
|
|||
|
I have Word documents that include tables with nested tables, and X number of nested tables within those nested tables, inconsistently from doc to doc. A lot of these tables are extending off the right side of the page. I'm looking for a macro to AutoFit Content all the tables and nested tables, so they fit on the page. I can get the main tables formatted fine, but from what I understand, Word doesn't consider nested tables as tables. And since the nested tables could be in any cell of the table above it, the macro would have to search through each cell to see if there's a table in the cell to format. I'm okay with some of the deep-nested tables being narrow in the end, as long as I can read its content.
I'm sure this is possible, I just need some guidance. Thank you! |
|
#2
|
||||
|
||||
|
Word does consider nested tables to be tables, the problem is just that the nested tables are contained inside other tables. For instance, to get to the first level of nesting
Code:
Sub TestTables()
Dim aTbl As Table, aTblN As Table
For Each aTbl In ActiveDocument.Tables
Debug.Print aTbl.NestingLevel
For Each aTblN In aTbl.Tables
Debug.Print aTbl.NestingLevel, aTblN.NestingLevel
Next aTblN
Next aTbl
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#3
|
|||
|
|||
|
Something like this might do for you:
Code:
Sub QuerryAllTables()
Dim oTbl As Table
Dim oRng As Range
For Each oTbl In fcnCollectDocTables
MsgBox "Cell count: " & oTbl.Range.Cells.Count & " - Nesting level: & oTbl.NestingLevel"
Next
lbl_Exit:
Exit Sub
End Sub
Function fcnCollectDocTables(Optional ByVal oDoc As Document) As Collection
'Returns all tables (top level and nested) in one collection.
Dim colStack As New Collection
Dim oTbl As Table
Set fcnCollectDocTables = New Collection
If Documents.Count > 0 And oDoc Is Nothing Then
Set oDoc = ActiveDocument
Else
GoTo lbl_Exit
End If
colStack.Add oDoc.Tables
Do While colStack.Count > 0
For Each oTbl In colStack(1)
fcnCollectDocTables.Add oTbl
If oTbl.Tables.Count > 0 Then colStack.Add oTbl.Tables
Next
colStack.Remove 1
Loop
lbl_Exit:
Exit Function
End Function
|
|
#4
|
|||
|
|||
|
You got it, gmaxey! Thanks so much!
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to compile text from multiple tables into a cell in a nested table | jrooney7 | Word VBA | 2 | 03-11-2019 07:55 AM |
| Removing KeepWithNext Formatting from Nested Tables? | Jfedora | Word VBA | 3 | 06-06-2017 11:49 AM |
| Nested tables. Count rows | NevilleT | Word VBA | 9 | 05-10-2017 05:22 AM |
| Formatting all tables | knightmetal | Word | 2 | 09-10-2012 10:53 PM |
| Nested vlookup with varable tables! | Dave Jones | Excel | 0 | 08-30-2012 09:15 AM |