Hi and thanks in advance for any help.
I am using the following code to automate a mail merge through excel at the click of a button.
If I take the IF statement out this code will save letters for everyone in the list, however I only want a letter to save if the offset value = ""
Any help would be greatly appreciated!
I am using the following code to automate a mail merge through excel at the click of a button.
Code:
Option Explicit
'change this to where your files are stored
Const FilePath As String = "C:\Users\chowell\Desktop\"
Dim wd As New Word.Application
Dim PersonCell As Range
Sub CreateWordDocuments()
'create copy of Word in memory
Dim doc As Word.Document
Dim Cell As Range
Set wd = New Word.Application
wd.Visible = True
Dim PersonRange As Range
'create a reference to all the people
Range("B3").Select
Set PersonRange = Range(ActiveCell, ActiveCell.End(xlDown))
'for each person in list �
For Each PersonCell In PersonRange
If PersonCell.Offest(0, 4).Value = "" Then
'open a document in Word
Set doc = wd.Documents.Open(FilePath & "Follow Up Letter.dotx")
'go to each bookmark and type in details
CopyCell "FirstName", 1
CopyCell "LastName", 0
CopyCell "Street", 6
CopyCell "City", 7
CopyCell "State", 8
CopyCell "Zip", 9
'save and close this document
doc.ExportAsFixedFormat OutputFileName:=FilePath & PersonCell.Value & ", " & PersonCell.Offset(0, 1) & ".PDF", _
ExportFormat:=wdExportFormatPDF
doc.Close False
Else
End If
Next PersonCell
wd.Quit
Set wd = Nothing
MsgBox "Created files in " & FilePath & "!"
End Sub
Sub CopyCell(BookMarkName As String, ColumnOffset As Integer)
'copy each cell to relevant Word bookmark
wd.Selection.GoTo What:=wdGoToBookmark, Name:=BookMarkName
wd.Selection.TypeText PersonCell.Offset(0, ColumnOffset).Value
End Sub
If I take the IF statement out this code will save letters for everyone in the list, however I only want a letter to save if the offset value = ""
Any help would be greatly appreciated!