Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2020, 09:17 AM
LBruce LBruce is offline Formatting Nested Tables Windows 10 Formatting Nested Tables Office 2013
Novice
Formatting Nested Tables
 
Join Date: Aug 2019
Posts: 9
LBruce is on a distinguished road
Default Formatting Nested Tables

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!
Reply With Quote
  #2  
Old 01-16-2020, 02:57 PM
Guessed's Avatar
Guessed Guessed is online now Formatting Nested Tables Windows 10 Formatting Nested Tables Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 2,631
Guessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to behold
Default

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
The next trick is to come up with a recursive function so that you can drill into deeper nesting levels. This thread vba - Find/Count all tables in the document's body (including all nested tables of all levels) - Stack Overflow has more inspiration for you.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 01-17-2020, 08:03 AM
gmaxey gmaxey is offline Formatting Nested Tables Windows 10 Formatting Nested Tables Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,204
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

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
Reply With Quote
  #4  
Old 01-20-2020, 09:07 AM
LBruce LBruce is offline Formatting Nested Tables Windows 10 Formatting Nested Tables Office 2013
Novice
Formatting Nested Tables
 
Join Date: Aug 2019
Posts: 9
LBruce is on a distinguished road
Default

You got it, gmaxey! Thanks so much!
Reply With Quote
Reply

Thread Tools
Display Modes


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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft