View Single Post
 
Old 12-09-2022, 03:48 AM
Chocomocco Chocomocco is offline Windows 10 Office 2021
Novice
 
Join Date: Dec 2022
Posts: 2
Chocomocco is on a distinguished road
Default How to run a macro on only specific tables in a mail merge export

Hello, a newbie here with almost zero VBA experience.

We have a mail merge template with a macro that does the following:
Step 1: generates the mail merge for all records in the data spreadsheet (creating a huge word document)
Step 2: deletes blank rows off the table(s).
Step 3: creates a separate PDF for each record (so we don't have one giant mail merged document but one for each person).

It works brilliantly on the mail merge template that has just one table.

Now I have a mail merge template that has 3 tables:
- Table 1: 5x5
- Table 2: 26 columns x 19 rows
- Table 3: 2x2

I want our 'removing blank rows' macro to only run on Table 2. Of course in the huge mail merge document created in Step 2, this is table 2 on the first page, but then will be table 5, table 8, table 11 (basically every 3rd table from table 2). It has the same identical table headings so it would be possible to search for specific text and then have that table selected. The mail merge document has data from about 200 records so it's fairly large (and time-consuming to delete blank rows manually).

We're using this macro code to delete the rows (taken from another post on this forum)...

Code:
Dim Tbl As Table, i As Long
With ActiveDocument
  For Each Tbl In .Tables
    With Tbl
      For i = .Rows.Count To 2 Step -1
        If Len(.Cell(i, 3).Range.Text) = 2 And Len(.Cell(i, 9).Range.Text) = 2 Then .Rows(i).Delete
      Next i
    End With
  Next Tbl
End With
The macro stops with an error on the line that's looking for specified columns, presumably because it's looking for column 9 and that doesn't exist in all the tables.

Any help to get my head around this problem would be great and thank you so much in advance.
Reply With Quote