Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-05
6
Medium Priority
?
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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 53

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 49

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Suggested Courses

660 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