Running Mail Merges From Script

 

The mail merging technology was embedded within the actions system and not exposed to script until 5.00.091. This build includes a script friendly interface to easily generate mail merges without requiring any pre-processing of the data sources into the expected format.

 A new object is exposed: MailMerge, which can be accessed from the script editor.

This object exposes a number of properties/methods: 

Name

Type

Description

NewDocument

Method

Called to instantiate a new merge document

KeySQL

Property

The SQL which generates the ID’s of the keys

Template

Property

The word template .DOT file

AppendDocument

Method

Append a document to collection

SaveAs

Property

Save the merged document as this file name

UserId

Property

Set the user who is running the merge

RunMailMerge

Method

Run the merge and generate the document

 The use of this technique is now described by use of an example:

 Private Sub MailMergeTest

    With MailMerge

       .NewDocument

       .KeySQL = "Select CompanyId, 26531 as ContactId, 99 as RequirementId, " & _

" 25 as ContractId, " & _

                  " 33518 as CandidateId, 4 as AdvertisementId, " & _

                  " 3 as ContractorInvoiceId, 1 as TestEntityId" & _

                  " From Company Where CompanyId = " & m_CompanyId

 

       .Template = "\\TriSys2000\ActionTemplates\client mailshot.dot"

       .AppendDocument "D:\CVData\P\Synonyms Test.doc"

       .SaveAs "\\TriSys2000\ActionTemplates\Mailshots\MMTest.rtf"

       .RunMailMerge

    End With

End Sub

 

The .KeySQL property accepts a SQL string returning only the keys of the underlying entities. In this example, we want to merge the document consisting of the current company (m_CompanyId), requirement 99, placement 25, candidate 33518, advertisement 4, contractor invoice 3 and test entity 1.

This allows you to control the relationships between the records. 

The .Template property is the full path to the word .dot file. 

.AppendDocument can be called any number of times to append any number of documents (CVs perhaps) to the end of the merge. 

The .SaveAs method tells the merge that the resulting merged file will be saved to the specified file name.

 The .RunMailMerge method starts the process of creating the data source (.tmm file), instantiating word, opening the .dot template and merging to a new document using the data source. If the .SaveAs file is set, it then saves the document. The resulting document is then made visible for viewing.