Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Oct 2002
    Location
    Stevenage, Bedfordshire, England
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Email Mail Merge from VB6 (VB6/Word2002)

    Hi,
    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:

    Run-time error '5603'
    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 it hasn't.
    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
    rng.Collapse Direction:=wdCollapseEnd
    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

    MainDoc.MailMerge.OpenDataSource Name:=sbDbaseName, _
    LinkToSource:=True, Connection:="DSN=WordLetters;DATABASE=" & sbDBPath _
    & ";UID=" & sbUserID & ";PWD=" & sbPWD, _
    SQLStatement:="SELECT * FROM WordMergeQueryEmail", subtype:=8 'wdMergeSubTypeWord2000

    MainDoc.MailMerge.Destination = wdSendToEmail

    With MainDoc.MailMerge.DataSource
    .FirstRecord = -1
    .LastRecord = -1
    End With

    MainDoc.MailMerge.Execute Pause:=False

    ' remove the temporary email field from the document
    ActiveDocument.Fields.Item(ActiveDocument.Fields.C ount).Delete


    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.

  2. #2
    New Lounger
    Join Date
    Oct 2002
    Location
    Stevenage, Bedfordshire, England
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Email Mail Merge from VB6 (VB6/Word2002)

    Well, I've found an answer to my question, partly that 'E-mail_Address' should have been 'Email_Address', and l've also added the following line:
    with objWord.MailMerge.DataSource
    .MappedDataFields(20)DataFieldIndex = .FieldNames('Email_Address')Index
    end with

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •