Programming mail merge processes

See also: Under the hood

If you look at the code below you will see most of the tricks you will need to integrate 4TOPS Mail Merge fully with your application. Adding similar code you no longer have to expose your users to the full 4TOPS Mail Merge interface, but can make a simpler version with everything (templates, processes) already prepared.

Below is an explanation of the code used in the Demo Database, notably in Form_Customer:

Thematically organized:

Notes:

'SelectedRecords is used to remember which records were selected
'when the selection is lost due to change of the focus when clicking a button
Private selrecsMailMerge As SelectedRecords

'ammCurrentProcesses makes available the processes and their objects as saved in [yourdb]_processes.xml 
Private
 ammCurrentProcesses As New accessmailmerge.CurrentProcesses

'The simplest way to run a process is by selecting it 'by name'
Private Sub cmdEmail_Click()
    ammCurrentProcesses("Email").Execute
    
'refreshes the Documents subform
    DocumentCreated
End Sub

'running a process from a listbox
Private Sub cmdMailMerge_Click()
    Dim strProcess As String
    If Not Len(lbxProcesses.Value) = 0 Then
        strProcess = lbxProcesses.Value
        RefreshProcesses
        ammCurrentProcesses(strProcess).Execute
    Else
        MsgBox "Select Mail Merge process"
    End If
    DocumentCreated
End Sub

Private Sub DocumentCreated()
'refresh the Documents subform (behind the documents tab)
    Me![Documents subform].Requery
End Sub

Private Sub GetProcesses()
 'update the listbox lbxProcesses with the list of available processes
     Dim varProcesses As Variant
    On Error GoTo HandleError
    varProcesses = ammCurrentProcesses().List(Me.Parent)
    If UBound(varProcesses) >= 0 Then
        lbxProcesses.RowSource = Join(varProcesses, ";")
    Else
        lbxProcesses.RowSource = ""
    End If
HandleExit:
    Exit Sub
HandleError:
    Resume HandleExit
End Sub

Public Sub RefreshProcesses()
'A Sub RefreshProcesses is called on the active form when the mail merge wizard
' has created a new process (if a Sub RefreshProcesses is available).

    'make sure the latest mail merge processes are from _processes.xml are used
    ammCurrentProcesses.Refresh

    'update the listbox lbxProcesses with the list of available processes
    GetProcesses
End Sub

'cleaning up variables
Private Sub Form_Close()
    Set selrecsMailMerge = Nothing
    Set selrecsProcesses = Nothing
    Set selrecsEmail = Nothing
    Set selrecsLetter = Nothing
    Set selrecsSubject = Nothing
End Sub

Private Sub Form_Load()
    'selrecs.. restores selected records if a (non-toolbar) control is clicked
    'Normally the record selection would get lost if the focus moves to
    'another control. Using selrecs.Init fixes this.
    
    Set selrecsMailMerge = New SelectedRecords
    'selrecsMailMerge.Init Me, cmdMailMerge (access 2007 split form)
    selrecsMailMerge.Init Parent.[Customers subform], cmdMailMerge
        
    'fill the listbox lbxProcesses with the list of available processes
    GetProcesses

End Sub

'Implement double-click: slect the preferred behaviour
Private Sub lbxProcesses_DblClick(Cancel As Integer)
    cmdMailMerge_Click
End Sub