Solved

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

Posted on 2014-03-05
6
511 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

832 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