I often received large documents with many, many tables. Most of the tables contain another table (i.e., nested table). I need to search
only the nested tables for rows containing certain text. If that text is found, I need to delete that row. Unfortunately, the text may also appear in the main table. I tried to write code (provided below) to do this; but it does not work. Given my VBA skillset, that is no surprise. I am hoping one of the experts, here, would take a look at it and point out the errors.
I have attached an example of the files that I receive. The main tables can be several rows and contain one or more nested tables plus text.
Code:
Sub SearchNestedTables()
'Search nested table rows for text and delete those rows
'My code does not work
Application.ScreenUpdating = False
Dim MainTable As Table
Dim NestedTable As Table
For Each MainTable In ActiveDocument.Tables
For Each NestedTable In MainTable.Tables
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "XXX text"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do
If .Information(wdWithInTable) = True Then
With NestedTable.Rows.Range
.Rows(1).Delete
End With
End If
.Collapse wdCollapseEnd
Loop While .Find.Execute
End With
Next NestedTable
Next MainTable
Application.ScreenUpdating = True
MsgBox "Done!", vbInformation
End Sub