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
<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
Here's the IF, THEN, ELSE statement method
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
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