Code generator

  1. Use the Mail Merge Wizard to make the initial process
  2. Open the Process Designer
  3. Select the process
  4. Make additional changes to the process's objects (process, database, data source, activities)
  5. Generate code menu: Process > Generate Code > Use process classes only

This would generate code as below which you can subsequently adapt.

Sub Exec_Email()
    Dim ammProcess As accessmailmerge.Process
    Dim ammDataSource1 As accessmailmerge.Datasource
    Dim ammDataSource2 As accessmailmerge.Datasource
    Dim ammMailMerge1 As accessmailmerge.MailMerge
    Dim ammSendMail1 As accessmailmerge.SendMail
    Dim ammTo1 As accessmailmerge.List
    Dim ammCC1 As accessmailmerge.List
    Dim ammBCC1 As accessmailmerge.List
    Dim ammDocument1 As accessmailmerge.Document
    Set ammProcess = New accessmailmerge.Process
    Set ammDataSource1 = New accessmailmerge.Datasource
    Set ammDataSource2 = New accessmailmerge.Datasource
    Set ammMailMerge1 = New accessmailmerge.MailMerge
    Set ammSendMail1 = New accessmailmerge.SendMail
    Set ammDocument1 = New accessmailmerge.Document
    With ammProcess
        .Name = "Email"
        .ProcessRecords = 0  '0 - Process Current Record; 1 - Process Selected Records; 2 - Process All Records
        .MainDataSourceName = "Query qryLettersOverview"
    End With
    With ammDataSource1
        .Name = "Query qryLettersOverview"
        Set .Application = Application
        .Type_ = "Query"
        .ObjectName = "qryLettersOverview"
    End With
    ammProcess.Datasources.Add ammDataSource1
    With ammDataSource2
        .Name = "Form Letters"
        Set .Application = Application
        .Type_ = "Form"
        .ObjectName = "Letters"
    End With
    ammProcess.Datasources.Add ammDataSource2
    ammDocument1.Name = "Email"
    ammProcess.Documents.Add ammDocument1, ammDocument1.Name
    With ammMailMerge1
        .InvokedApplication = "Microsoft Word"
        .Template = "D:\projects\agora\Access\accessmailmerge\templates\Letter.dot"
        .DatasourceName = "Form Letters"
        .OutputDocumentName = "Email"
        .ShowDocument = False
        .MailMergeType = 3 '0 - Document; 1 - Listing; 2 - Labels
    End With
    ammProcess.Activities.Add ammMailMerge1
    With ammSendMail1
        .InputDocumentName = "Email"
        Set ammTo1 = New accessmailmerge.List
        ammTo1.ValueList = "[Field:CustomerEmail]"
        .SendMethod = 1 '0 - Send; 1 - Display; 2 - Save
        .EmailDocumentAs = 1 '0 - As Attachment; 1 - As HTML; 2 - As Text; 3 - None
    End With
    ammProcess.Activities.Add ammSendMail1
    ammProcess.Execute
    ammProcess.Dispose
    Set ammDataSource1 = Nothing
    Set ammDataSource2 = Nothing
    Set ammMailMerge1 = Nothing
    Set ammSendMail1 = Nothing
    Set ammTo1 = Nothing
    Set ammCC1 = Nothing
    Set ammBCC1 = Nothing
    Set ammDocument1 = Nothing
    Set ammProcess = Nothing
           
End Sub