Word

Automating Mail Merge

Ease of Use

Easy

Version tested with

2000 

Submitted by:

mdmackillop

Description:

Creates the merged document automatically when the mail merge document is opened and closes down the main document. 

Discussion:

Creates the final merged document when the mailmerge is called from another application, (typically Access or Excel), once the data has been written. Once the Public Sub below has been saved in Normal.dot, any mailmerge document can be automated by adding the DoMailMerge code to the MailMerge main document as detailed. 

Code:

instructions for use

			

Private Sub Document_Open() DoMailMerge End Sub Public Sub DoMailMerge() Dim DocName$ DocName = ActiveDocument.Name 'Do mailmerge With ActiveDocument.MailMerge .Destination = wdSendToNewDocument .Execute End With 'Close mailmerge document Windows(DocName).Close wdDoNotSaveChanges End Sub

How to use:

  1. Copy the code above.
  2. Open the MailMerge document.
  3. Hit Alt+F11 to open the Visual Basic Editor.
  4. Double-click Project Normal at the left.
  5. Choose Insert-Module, and paste the Public Sub code into the window at the right.
  6. Hit the Save diskette button and close the VBE.
  7. Double-click the MailMerge Project at the left.
  8. Double-click MicrosoftWordObjects.
  9. Double-click ThisDocument
  10. Paste the Private Sub code into the window at the right.
  11. Hit the Save diskette button and close the VBE.
  12. To edit the document or the code, hold down Shift when opening the document to prevent the macro from running
 

Test the code:

  1. Close and reopen the mailmerge document. A new FormLetters document should be created containing the merged information.
 

Sample File:

No Attachment 

Approved by Anne Troy


This entry has been viewed 274 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express