Results 1 to 2 of 2

Thread: Mail Merge (XP)

  1. #1
    New Lounger
    Join Date
    Jan 2003
    Location
    Ocean, New Jersey, USA
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mail Merge (XP)

    Have successfully created a mail merge document that can be invoked from a command button in Access, using the following VBA code on click (borrowed from Alson Balter's "Mastering Access 2002":

    If CreateWordObj() Then

    'If Word object created
    With gobjWord

    'Make Word visible
    .Visible = True

    'Open a document called DocumentName in the
    'current folder
    .Documents.Open CurrentProject.Path & _
    "DocumentName.doc"

    'Give the document time to open
    DoEvents

    'Use the MailMerge method of the document
    'to perform a mail merge
    With gobjWord.ActiveDocument.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    .Execute
    End With

    'Send the result of the merge to the print preview
    'window
    .ActiveDocument.PrintPreview 'Preview

    'Make Word visible
    .Visible = True
    End With
    End If

    Works fine, except that , IN ADDITION TO the preview copy of the letter displayed, TWO copies of the (master?) letter appear. When I go to close the two additional copies, a prompt asks whether I want to save changes.
    One copy (in addition to the preview) might be nice to permit changes to the text for a single invocation, but two seems excessive, and I don't understand why anything other than the preview is displayed. Have played with the VBA code and tried to follow several related(?) threads here on the Lounge (some seem a little beyond my capability to follow yet), but no progress so far.
    Any less complicated solutions out there, or an explanation for why the two copies appear?
    Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Mail Merge (XP)

    I don't understand why two copies of the same document would be displayed. The code you posted opens a document (.Documents.Open ...), and proceeds to execute a mail merge to a new document (.Activedocument.MailMerge.Execute); the mail merge will leave the master document open and create a merged document; the latter is shown in Print Preview. This is exactly the same behavior as with an interactive mail merge - if you merge to a new document, the master document is left open. If you don't want this, you should close it explicitly. See following code; I indicated changes in bold. I removed the second .Visible = True - it is superfluous since you already made the Word application visible.

    Dim objDoc As Word.Document

    If CreateWordObj() Then
    'If Word object created
    With gobjWord
    'Make Word visible
    .Visible = True

    'Open a document called DocumentName in the
    'current folder and assign to objDoc
    Set objDoc = .Documents.Open(CurrentProject.Path & _
    "DocumentName.doc")


    'Give the document time to open
    DoEvents

    'Use the MailMerge method of the document
    'to perform a mail merge
    With objDoc.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    .Execute
    End With
    'Close merge document without saving changes
    objDoc.Close SaveChanges:=False

    'Send the result of the merge to the print preview
    'window
    .ActiveDocument.PrintPreview 'Preview
    End With
    End If

Posting Permissions

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