Hi all:
I have an app that merges data from an Excel spreadsheet into a form letter in Word. This works great as long as there is more than one record. With one record, I get two docs, and with no data I get one doc.
If I do it manually, it works fine.
Here's my code:
Any help will be greatly appreciated.
Thanks,
Ron Repp
If gray hair is a sign of wisdom, then I'm a genius.
My newest novel: Wooden Warriors
I have an app that merges data from an Excel spreadsheet into a form letter in Word. This works great as long as there is more than one record. With one record, I get two docs, and with no data I get one doc.
If I do it manually, it works fine.
Here's my code:
Code:
Public Sub MergeDoc(ByVal DocName As String, ByVal SaveName As String)
If WD.Visible = False Then WD.Visible = True
OpenDocument (DocName)
With WD
With .ActiveDocument.MailMerge
.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
.Execute
End With
On Error GoTo NoFile
End With
SaveAs (SaveName)
Select Case SaveName
Case DefPath & "Letters\JuvExpired" & sM & sD & sY & ".doc"
Expired = CountPages()
InsertData "EXPIRED", "LETTER", Expired, "JuvExpired" & sM & sD & sY & ".doc"
Case DefPath & "Letters\JuvDecline" & sM & sD & sY & ".doc"
Denied = CountPages()
InsertData "DECLINED", "LETTER", Denied, "JuvDecline" & sM & sD & sY & ".doc"
Case DefPath & "Letters\JuvInvalid" & sM & sD & sY & ".doc"
Invalid = CountPages()
InsertData "INVALID", "LETTER", Invalid, "JuvInvalid" & sM & sD & sY & ".doc"
End Select
WD.DisplayAlerts = wdAlertsNone
WD.ActiveDocument.Close (False)
Exit Sub
NoFile:
WD.ActiveDocument.Close (False)
End Sub
=============================
Public Sub OpenDocument(ByVal FileName As String)
WD.Documents.Open FileName, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=Word.WdOpenFormat.wdOpenFormatAuto
End Sub
Any help will be greatly appreciated.
Thanks,
Ron Repp
If gray hair is a sign of wisdom, then I'm a genius.
My newest novel: Wooden Warriors