I am trying to write a Word mail merge application in Visual Basic 6. The 'normal' merge to a new document goes fine, but l
also need to merge some documents to email - where the patient has an email address.
The email merge goes fine up to the actual 'execute' statement where l get the following error message:
Word cannot merge documents that can be distributed by mail or fax without a valid mail address. Choose the Setup button to
select a mail address data field.
If l then switch to the Word document, and display the 'Mail Merge Wizard', it's
got to step 3, where you select recipients. l then click on the 'Match Fields' button on the MailMerge toolbar, and l can see
that my email field has been selected as the email address, even though the error message above seems to be indicating that
Below is the code l am using:
'add an email field to the beginning of the doc
Dim rng As Word.Range
Set rng = ActiveDocument.Range
ActiveDocument.Fields.Add rng, wdFieldMergeField, """E-mail_Address"" m"
'start the email mail merge
MainDoc.MailMerge.MailAddressFieldName = "E-mail_Address"
MainDoc.MailMerge.MailSubject = "WordLetters Email"
MainDoc.MailMerge.MainDocumentType = 4 'wdEMail
LinkToSource:=True, Connection:="DSN=WordLetters;DATABASE=" & sbDBPath _
& ";UID=" & sbUserID & ";PWD=" & sbPWD, _
SQLStatement:="SELECT * FROM WordMergeQueryEmail", subtype:=8 'wdMergeSubTypeWord2000
MainDoc.MailMerge.Destination = wdSendToEmail
.FirstRecord = -1
.LastRecord = -1
' remove the temporary email field from the document
The only thing that isn't obvious from the code
is that there is a reference in the project to Word 9, but because l need to run this in either 2000 (Word 9) or 2002 (Word
10), l have used late binding in the procedure to make the program use the correct reference, so that l can add, for example,
'subtype' to the OpenDataSource without VB having a wobbly.