Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-09-2022, 03:48 AM
Chocomocco Chocomocco is offline How to run a macro on only specific tables in a mail merge export Windows 10 How to run a macro on only specific tables in a mail merge export Office 2021
Novice
How to run a macro on only specific tables in a mail merge export
 
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
  #2  
Old 12-09-2022, 04:55 AM
macropod's Avatar
macropod macropod is offline How to run a macro on only specific tables in a mail merge export Windows 10 How to run a macro on only specific tables in a mail merge export Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,927
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:
Dim s As Long, i As Long
With ActiveDocument
  For s = 1 To .Sections.Count - 1
    With Sections(s).Range.Tables(2)
      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
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-10-2022, 02:39 AM
Chocomocco Chocomocco is offline How to run a macro on only specific tables in a mail merge export Windows 10 How to run a macro on only specific tables in a mail merge export Office 2021
Novice
How to run a macro on only specific tables in a mail merge export
 
Join Date: Dec 2022
Posts: 2
Chocomocco is on a distinguished road
Default

Thanks so much, I'll give this a whirl when I'm back at work on Monday.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to separate mail merge tables Tesla Mail Merge 1 05-05-2022 06:55 AM
How to run a macro on only specific tables in a mail merge export Export .csv file to Word for Mail Merge? Jshopping Mail Merge 3 09-22-2018 11:04 PM
Update & Unlink Specific Merge Field in Word Doc from Mail Merge - Excel VBA RMerckling Mail Merge 16 05-17-2018 05:19 PM
How to run a macro on only specific tables in a mail merge export how do i ad a specific pdf file to a mail merge? mseibel Mail Merge 1 08-28-2012 07:53 PM
How to run a macro on only specific tables in a mail merge export specific mail merge cmccabe Mail Merge 3 06-14-2012 04:12 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:38 AM.


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