I have an excel workbook that contains columns for name and a decision. If the value of the decision column equals "keep", I want to create a macro that auto-populates the name into a word template that will eventually become a letter mailed to somebody. Here is the table and my code so far:
Name | Decision |
---|---|
John Doe | Keep |
Jane Dow | Remove |
Sub letter()
Dim rng As Range
Dim row As Range
Dim cell As Range
Dim i As Integer
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.Add(Template:="file name here", NewTemplate:=False, DocumentType:=0)
ActiveSheet.Select
For i = 2 To 3
If Cells(i, 3) = "Keep" Then
With wDoc
.Application.Selection.Find.Text = "..name.."
.Application.Selection.Find.Execute
.Application.Selection = Cells(i, 1)
.Application.Selection.EndOf
ChangeFileOpenDirectory "Directory goes here"
ActiveDocument.SaveAs2 Filename:="file name here" _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
End With
End If
Next i
End Sub
The excel workbook looks something like this:
For each row, if the 3rd column value equals "keep", I would like a letter to be generated and saved as "letter_customername.docx", where "customer name" is autopopulated based on the workbook. Additionally, I would like the letter to begin with "Dear 'Customer Name'", where the customer name is again generated from the workbook. What I have as a template is something like this:
Dear ..Name..
Body
Closing
I am envisioning a macro where "..name.." is replaced with the customer name from the workbook.
The code above does not seem to accomplish this. I am stuck and not sure how to proceed. Any assistance would be greatly appreciated!
Note my code is borrowed from here