![]() |
|
|
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Quote:
![]() that gives ![]() which looks incorrect to me. |
|
#4
|
||||
|
||||
|
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] |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
Quote:
![]() your code augmented: Quote:
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 |
|
#7
|
|||
|
|||
|
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?
|
|
#8
|
|||
|
|||
|
Accessing its cell content - as macropod's https://www.msofficeforums.com/187071-post4.html almost achieves.
|
|
#9
|
|||
|
|||
|
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
|
|
#10
|
|||
|
|||
|
Great! Thanks.
|
|
#11
|
|||
|
|||
|
Aha. Thanks.
What do the bullets characters represent? |
|
#12
|
||||
|
||||
|
They represent the end-of-cell and end-of-row characters.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |