VBA Automated Mailmerge using 2 templates based on cell value

jansson17

New Member
Joined
May 6, 2015
Messages
1
Scenario:
I have a spreadsheet used for generating letters via an automated mail merge macro. The spread typically contains about 2000 rows
Problem:
I need to have the ability to create letters using 2 different letter templates based on cell values in a column. In the example below, the value on column C should dictate which letter template will be used for each row.
Example

Col A
Col B
Col C
John
Smith
YES
Letter Template 1 to be used
Joe
Henricks
NO
Letter Template 2 to be used
Mary
Jones
YES
Letter Template 1 to be used

<tbody>
</tbody>



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Here is some VBA I was playing with but can't quite get it working for the 2 different letters.
I've also tried using IF, THEN, ELSE statements but still can't get it working
Code:
       Sub CommandButton2_Click()
       Selection.AutoFilter   '''''''''' This should filter all rows based on the YES value 
        ActiveSheet.Range("D1:AH1").AutoFilter Field:=31, Criteria1:= _
        "YES"
         '''''''''''''''''''''''''''''''''''''''''
 
       Dim WordApp As Object
       Dim rng As Range
       Range("A1:H1").Select
 
        Set rng = Application.Intersect(ActiveSheet.UsedRange, Range("D1:AH1"))
        rng.SpecialCells(xlCellTypeVisible).Select

       On Error Resume Next
       Set WordApp = GetObject(, "Word.Application")
       On Error GoTo 0
       If WordApp Is Nothing Then
       Set WordApp = CreateObject("Word.Application")
       End If
        ''' This should run the macro using the YESletter Template
               WordApp.Visible = False
       WordApp.Documents.Open "[URL="file://\\....\docs\lg\Letterbuilder\YESletter.docm"]\\....\docs\lg\Letterbuilder\YESletter.docm[/URL]""
       WordApp.Run "Module1.SaveIndividualWordFiles"
        
       '''''''''''''''''''''''''''''''''''''''''
  
       Selection.AutoFilter   '''''''''' This should filter all rows        based        on        the NO value 
    ActiveSheet.Range("D1:AH1").AutoFilter Field:=31, Criteria1:= _
        "Post"
       '''''''''''''''''''''''''''''''''''''''''
 
       On Error Resume Next
       Set WordApp = GetObject(, "Word.Application")
       On Error GoTo 0
       If WordApp Is Nothing Then
       Set WordApp = CreateObject("Word.Application")
       End If
        ''' This should run the macro using the NOletter Template
               WordApp.Visible = False
       WordApp.Documents.Open "[URL="file://\\....\docs\lg\Letterbuilder\NOletter.docm"]\\....\docs\lg\Letterbuilder\NOletter.docm[/URL]"
       WordApp.Run "Module1.SaveIndividualWordFiles"
       End
Here's the IF, THEN, ELSE statement method
Code:
       If ThisWorkbook.Sheets("LetterData").Range("AH").Value = "YES" Then
           WordApp.Visible = False
       WordApp.Documents.Open "[URL="file://\\....\docs\lg\Letterbuilder\YESletter.docm"]\\....\docs\lg\Letterbuilder\YESletter.docm[/URL]"
       WordApp.Run "Module1.SaveIndividualWordFiles"
       ELSE
           WordApp.Visible = False
       WordApp.Documents.Open "[URL="file://\\....\docs\lg\Letterbuilder\NOletter.docm"]\\....\docs\lg\Letterbuilder\NOletter.docm[/URL]"
       WordApp.Run "Module1.SaveIndividualWordFiles"
       End
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,215,575
Messages
6,125,612
Members
449,238
Latest member
wcbyers

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top