Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-10-2025, 04:08 PM
chrisjj chrisjj is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 7 64bit Word 2003: Has anyone had success in accessing nested tables in a macro? Office 97-2003
Competent Performer
Word 2003: Has anyone had success in accessing nested tables in a macro?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default Word 2003: Has anyone had success in accessing nested tables in a macro?

Not me. Even the simplest task fails for me. The attached has no nested tables yes VBA reports it has one.

Code:
Option Explicit

Sub CountFirstLevelNestedRows()
    Dim tbl As Table
    Dim rw As row
    Dim c As cell
    Dim nestedCount As Long
    Dim nt As Table
    
    nestedCount = 0
    
    ' Loop through all tables in the document
    For Each tbl In ActiveDocument.Tables
        ' Check each row for nested tables
        For Each rw In tbl.Rows
            For Each c In rw.Cells
                ' If this cell contains nested tables (first-level)
                If c.Range.Tables.count > 0 Then
                        MsgBox "Nested table !"
                End If
            Next c
        Next rw
    Next tbl
End Sub




Any help appreciated before I spend more time up what looks like a garden path...

Thanks.
Attached Files
File Type: doc Nested table detect FALSE +VE!.doc (43.0 KB, 3 views)
Reply With Quote
  #2  
Old 09-10-2025, 04:54 PM
macropod's Avatar
macropod macropod is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 10 Word 2003: Has anyone had success in accessing nested tables in a macro? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For example:
Code:
Sub Demo()
Dim Tbl As Table, Cll As Cell, Rng As Range
For Each Tbl In ActiveDocument.Tables
  With Tbl
    For Each Cll In .Range.Cells
      Set Rng = Cll.Range: Rng.End = Rng.End - 1
      If Rng.Cells(1).NestingLevel > 1 Then
        With Rng
          MsgBox .Text
          .End = .End - 2: .Start = .Characters.Last.Cells(1).Range.Start
          MsgBox .Text
        End With
      End If
    Next
  End With
Next
End Sub
But this only works if there is no other content in the parent cell.

Alternatively, you could use:
Code:
Sub Demo()
Dim Tbl As Table, Cll As Cell
For Each Tbl In ActiveDocument.Tables
  With Tbl
    For Each Cll In .Range.Cells
      With Cll.Range
        If .Cells(1).Tables.Count > 0 Then
          With .Cells(1).Tables(1).Range
            MsgBox .Text
            .End = .End - 2: .Start = .Characters.Last.Cells(1).Range.Start
            MsgBox .Text
          End With
        End If
      End With
    Next
  End With
Next
End Sub
which works even if there is other content in the parent cell.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-10-2025, 09:34 PM
Guessed's Avatar
Guessed Guessed is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 10 Word 2003: Has anyone had success in accessing nested tables in a macro? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 09-13-2025, 05:00 AM
chrisjj chrisjj is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 7 64bit Word 2003: Has anyone had success in accessing nested tables in a macro? Office 97-2003
Competent Performer
Word 2003: Has anyone had success in accessing nested tables in a macro?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Alternatively, you could use:
Code:
Sub Demo()
Dim Tbl As Table, Cll As Cell
For Each Tbl In ActiveDocument.Tables
  With Tbl
    For Each Cll In .Range.Cells
      With Cll.Range
        If .Cells(1).Tables.Count > 0 Then
          With .Cells(1).Tables(1).Range
            MsgBox .Text
            .End = .End - 2: .Start = .Characters.Last.Cells(1).Range.Start
            MsgBox .Text
          End With
        End If
      End With
    Next
  End With
Next
End Sub
which works even if there is other content in the parent cell.
On



that gives



which looks incorrect to me.
Reply With Quote
  #5  
Old 09-13-2025, 05:13 AM
chrisjj chrisjj is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 7 64bit Word 2003: Has anyone had success in accessing nested tables in a macro? Office 97-2003
Competent Performer
Word 2003: Has anyone had success in accessing nested tables in a macro?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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
Reply With Quote
  #6  
Old 09-13-2025, 11:51 AM
vivka vivka is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 7 64bit Word 2003: Has anyone had success in accessing nested tables in a macro? Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

Hi! What do you mean by "access the nested table"? Selecting the nested tbl or getting the row-column info about each cell of the nested tbl, or getting the info about the parent tbl's cell that has a nested tbl?
Reply With Quote
  #7  
Old 09-13-2025, 01:01 PM
chrisjj chrisjj is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 7 64bit Word 2003: Has anyone had success in accessing nested tables in a macro? Office 97-2003
Competent Performer
Word 2003: Has anyone had success in accessing nested tables in a macro?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default

Accessing its cell content - as macropod's https://www.msofficeforums.com/187071-post4.html almost achieves.
Reply With Quote
  #8  
Old 09-14-2025, 04:01 AM
gmaxey gmaxey is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 10 Word 2003: Has anyone had success in accessing nested tables in a macro? Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Code:
Sub QuerryAllTables()
Dim oTbl As Table
Dim oRng As Range
Dim oCell As Cell
  For Each oTbl In fcnCollectDocTables
    If oTbl.NestingLevel > 1 Then
      For Each oCell In oTbl.Range.Cells
        If Len(oCell.Range.Text) > 2 Then
          Debug.Print Left(oCell.Range.Text, Len(oCell.Range.Text) - 2)
        End If
      Next oCell
    End If
  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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 09-14-2025, 01:03 PM
chrisjj chrisjj is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 7 64bit Word 2003: Has anyone had success in accessing nested tables in a macro? Office 97-2003
Competent Performer
Word 2003: Has anyone had success in accessing nested tables in a macro?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default

Great! Thanks.

Reply With Quote
  #10  
Old 09-14-2025, 06:21 PM
macropod's Avatar
macropod macropod is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 10 Word 2003: Has anyone had success in accessing nested tables in a macro? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by chrisjj View Post
On



that gives



which looks incorrect to me.
That is, in fact, correct. What the message box is reporting is firstly the raw content of the nested table and secondly the trimmed content of the last cell in that table.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 09-15-2025, 01:52 AM
chrisjj chrisjj is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 7 64bit Word 2003: Has anyone had success in accessing nested tables in a macro? Office 97-2003
Competent Performer
Word 2003: Has anyone had success in accessing nested tables in a macro?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default

Aha. Thanks.

What do the bullets characters represent?
Reply With Quote
  #12  
Old 09-15-2025, 02:28 PM
macropod's Avatar
macropod macropod is offline Word 2003: Has anyone had success in accessing nested tables in a macro? Windows 10 Word 2003: Has anyone had success in accessing nested tables in a macro? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

They represent the end-of-cell and end-of-row characters.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2003: Has anyone had success in accessing nested tables in a macro? Search in Nested Tables to Delete Rows JingleBelle Word VBA 6 11-13-2020 07:36 AM
Formatting Nested Tables LBruce Word VBA 3 01-20-2020 09:07 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
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 01:27 PM.


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