Running an Export with Macros for Multiple Participants

When running an export for a Word Document that contains a macro, DB Precision has a feature which will allow it to run that macro upon opening that document.

If that same export is run for multiple participants where each document is to be stored in the archive, DB Precision will run the macro separately for each participant.

However, if that export is run and not stored in the archive, the macro will only be run once for the first participant (when the document is opened) and not run again. While this is fine when running an export for an individual, it can be a problem when running an export for a group (such as when running benefit statements).

To have Word run a macro separately for each record, the following must be done:

Create a Class Module
In order to create the loop necessary to run through multiple records with your Macro, you must first create a Class Module in Word's VBA (Visual Basic for Applications). For more information on opening visual basic, click here.
  • Make Sure that Project Explorer is Viewable: on the VBA menu, select View - Project Explorer
  • Add Class Module: under the project for your document, right-click Class Modules and select Insert - Class Module.
  • Rename the Newly Created Module: a new module named "Class1" will have been created. To rename this, select Class1 in Project Explorer and then, in the Properties window (window below Project Explorer), change the name to "MergeModules".
Insert Code into Class Module
  • Set Defaults for Module: add the lines "Option Explicit" and "Public WithEvents wdApp As Word.Application" to your document (see below).
  • Add Merge Event Handlers:  there are four possible events that can be handled with merges: before entire merge (wdApp_MailMergeBeforeMerge), after entire merge (wdApp_MailMergeAfterMerge), before a record is merged (wdApp_MailMergeBeforeRecordMerge), and after a record is merged (wdApp_MailMergeAfterRecordMerge). These can be added as shown below with Private Sub and End Sub.
In the following example, screen updating is turned off for both Word and the underlying chart data (to speed up merge processing in Word), chart values are updated with each record, and then screen updating is turned back on after the entire merge is completed.
Please note that these events are only triggered during an actual merge and are not triggered when flipping through records within Word.


Attach Class Module Code to Document
  • Create Variable for Merge Modules in Document Code: add the line "Dim myMergeModules As MergeModules" to your document's VB code (see below).
  • Set the Variable: within the subroutine that is run by DB Precision on Document Open (usually named PSMacro), set the Class Modules variable.


When running the mail merge for this document, you will now see that the macros defined in the Class Module will also run.
Prior Next


PensionSoft Corporation | 860.540.3690 | support@pensionsoft.com