Solved

How to trigger codes to run each time a record mailmerge(MailMergeAfterRecordMerge)

Posted on 2014-03-05
6
498 Views
Last Modified: 2014-04-05
I have some code that normally run on the document close event to register the document into a document management system. It works fine on a single document.

I now want to use it in a mail merge and run it for each record merge. The user will run the mail merge normally from the toolbar.

For each record that merges I want to pull some data from the mail merge data source and use it to register the document before merging the next record.

It seems that the MailMergeAfterRecordMerge is the event that I need to use but I don't quiet understand how to make it trigger.
0
Comment
Question by:Sheils
  • 2
  • 2
6 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39905948
Hi,

the only event controlling the ResultDocument is MailMergeAfterMerge

see my example

It creates a custom processing in the mail merge wizard, which merges each Record separately and the save it in a file
Dim WithEvents wdapp As Application
Dim bCustomProcessing As Boolean

Private Sub Document_Open()

Set wdapp = Application
bCustomProcessing = False
ThisDocument.MailMerge.DataSource.ActiveRecord = 1
ThisDocument.MailMerge.ShowWizard 1
With ActiveDocument.MailMerge
   If .MainDocumentType = wdFormLetters Then
       .ShowSendToCustom = "Custom Letter Processing"
   End If
End With

End Sub
Private Sub wdapp_MailMergeWizardSendToCustom(ByVal Doc As Document)

bCustomProcessing = True
Doc.MailMerge.Destination = wdSendToNewDocument
With Doc.MailMerge
    For rec = 1 To .DataSource.RecordCount
        .DataSource.ActiveRecord = rec
        .DataSource.FirstRecord = rec
        .DataSource.LastRecord = rec
        .Execute
    Next
End With

MsgBox "Merge Finished"
End Sub


Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)
If bCustomProcessing = True Then
    With Doc.MailMerge.DataSource.DataFields
        sFirmPathName = .Item(1).Value ' First Column of the data
    End With
    DocResult.SaveAs "c:\Documents\" & sFirmPathName & "\MailDocument.docx", wdFormatXMLDocument
    DocResult.Close False
End If
End Sub

Open in new window

Regards
0
 
LVL 16

Author Comment

by:Sheils
ID: 39907230
Hi Rgonzo

Your code works well when merging all records from the recordsource. However it generates an error in line 39 (DocResult.SaveAs "C:\Users\Sheils\Documents\testmerge\" & sFirmPathName & "MailDocument.docx", wdFormatXMLDocument) when you uncheck some of the records in edit recipient list. The error occurs just after the first unchecked record.



Please note that I have added a new line after your line 9 to move the mail wizard to step 6 so that the user don't have to move through the steps each time they use the mail merge.
0
 
LVL 16

Author Comment

by:Sheils
ID: 39907236
The code that I have included after your line 9 is

ThisDocument.MailMerge.WizardState = 6
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39917597
Then you will have to go the AfterProduction way

by saving the resulting Doc divided by sections

http://answers.microsoft.com/en-us/office/forum/office_2003-word/use-mail-merge-to-create-multiple-documents/a1f3c40f-36ab-4b4e-ad67-1ee09d7d84a8

Regards
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39980741
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now