LanceS2000
LanceS2000

Reputation: 21

How to reference all tables in a Word document?

I need to execute VBA code on multiple tables in Word.

I tried referencing/adding numbers to add more tables to execute the script on at the ActiveDocument.Tables(1) line.

I don't know how to reference all tables 1 through 4.

I am left to duplicate the macro below and enter "(2)", "(3)", "(4)" for the ActiveDocument.Tables number to reach all of the tables.

With ActiveDocument.Tables(1)
    For r = .Rows.Count To 1 Step -1
        fnd = False
        For Each c In .Rows(r).Cells
            If InStr(c.Range.Text, "ARORA") > 0 Then fnd = True
        Next
        For Each c In .Rows(r).Cells
            If InStr(c.Range.Text, "Description of Change") > 0 Then fnd = True
        Next
        If Not fnd Then .Rows(r).Delete
    Next
End With
End Sub

The code removes rows from the specified table which contain the specified text "c.Range.Text". This executes only for the one table specified at ActiveDocument.Tables (i.e., "(1)", etc.).

I need this to run for all 4 tables, 1, 2, 3, and 4 in my Word document.

Upvotes: 0

Views: 1000

Answers (1)

LanceS2000
LanceS2000

Reputation: 21

For Each Table In ActiveDocument.Tables
    For r = Table.Rows.Count To 1 Step -1
        fnd = False
        For Each c In Table.Rows(r).Cells
            If InStr(c.Range.Text, "PartnerAbbr") > 0 Then fnd = True
        Next
        For Each c In Table.Rows(r).Cells
            If InStr(c.Range.Text, "Description of Change") > 0 Then fnd = True
        Next

Upvotes: 2

Related Questions