I am attempting to automate a mail merge in Word 2003 using VBScript. I have created a main document for the merge and when I click Merge to New Document I get a new document called Envelopes1 with the correct number of envelopes and the correct address
block data. When I execute the VBScript below, I get a new document with the correct number of envelopes, but the address block data is not there. The main document and the VBScript use the same datasource file, so I do not think it is a matter
of the datasource file contents/format changing. I have been executing this code in VBSEdit and DTS with the same results. (I know this is old technology, but it is what I am stuck with right now.)
I've been beating my head against this for 2 days. Any ideas?
Here's the code:
Dim WordFileTemplateName
Dim WordFileOutputName
Dim appword
Dim strDataSourceName
strDataSourceName = "\\Endeavor\Public\ZInspectionOrders\SOI\SOIMailMergeDocs\SOI_5yrInsp_201101_MailList.csv"
'DTSGlobalVariables("ExportBackup").Value & "SOI\SOIMailMergeDocs\" & DTSGlobalVariables("FileName").Value & "_MailList.csv"
WordFileTemplateName = "\\Endeavor\Public\ZInspectionOrders\Template\SOIMailMergeTemplate.doc"
WordFileOutputName = "\\Endeavor\Public\ZInspectionOrders\SOI\SOIMailMergeDocs\SOIMailMerge_" _
& Year(Now()) & Right("00" & Month(Now()),2) & Right("00" & Day(Now()),2) & ".doc"
Set appword = CreateObject("word.application")
appword.Visible = True
appword.DisplayAlerts = 0
appword.Documents.Open WordFileTemplateName
With appword.ActiveDocument.MailMerge
.MainDocumentType = 2
.Destination = 0
.OpenDataSource strDataSourceName,5,False,False,True,,,,,,,,,,,0
'.MailAsAttachment = False
'.MailAddressFieldName = ""
'.MailSubject = ""
.SuppressBlankLines = True
.Execute Pause=True
End With
With appword.ActiveDocument
.SaveAs(WordFileOutputName)
End With
appword.Quit (False) 'without saving
set appword = nothing