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
Mark Jones YES Letter Template 1 to be used
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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
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 "\\....\docs\lg\Letterbuilder\YESletter.docm""
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 "\\....\docs\lg\Letterbuilder\NOletter.docm"
WordApp.Run "Module1.SaveIndividualWordFiles"
End
Here's the IF, THEN, ELSE statement method
If ThisWorkbook.Sheets("LetterData").Range("AH").Value = "YES" Then
WordApp.Visible = False
WordApp.Documents.Open "\\....\docs\lg\Letterbuilder\YESletter.docm"
WordApp.Run "Module1.SaveIndividualWordFiles"
ELSE
WordApp.Visible = False
WordApp.Documents.Open "\\....\docs\lg\Letterbuilder\NOletter.docm"
WordApp.Run "Module1.SaveIndividualWordFiles"
End