Full Version: Filtered mail merge from form
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
cglew
Hi,

I have a form, to which a filter has been applied. Is it possible to take the filtered results (and only the filtered results) and have them exported to a Microsoft Word 2007 mail merge document, that has already been set up, via a command button?

Many thanks.
kapeller
Hi Chris

I can across this code that I have used in the past.

It may get you started.

CODE
Private Sub cmdPrintLetter_Click()
On Error Resume Next
    Dim db As DAO.Database
    Dim objWord As Word.Application
    Dim TemplateChoice As String
    Dim strMyTemplatePath As String
    
    If IsNull(txtStartDate) Or IsNull(txtEndDate) Then
        MsgBox "You must enter the start date certificates were issued," & vbCr & vbCr & _
        "you must enter the end date certificates were issued" & vbCr & vbCr & _
        "and you should click the Search button first!", vbOKOnly + vbCritical, "Date Certificates Were Issued"
       DoCmd.GoToControl "txtStartDate"
     Else

        DoCmd.SetWarnings False
        DoCmd.OpenQuery "qryNCoCInterimApprovalNLSMergeTemplate"
        DoCmd.SetWarnings True

            Set db = CurrentDb()

            Set objWord = GetObject(, "Word.Application")
            If Err.Number <> 0 Then
            Set objWord = CreateObject("Word.Application")
            End If

            objWord.Visible = True
            
            'Code here to allow choice of Template
            strMyTemplatePath = Access.CurrentProject.Path & "\Template Letters\OHS 162 L NCoC interim approval NLS merge template.dot"

            objWord.Documents.Add Template:=strMyTemplatePath, NewTemplate:=True

                    objWord.ActiveDocument.MailMerge.OpenDataSource _
                            Name:=db.Name, _
                            LinkToSource:=True, _
                            Connection:="TABLE tblTempNCoCInterimApprovalNLSMergeTemplate", _
                            SQLStatement:="Select * from [tblTempNCoCInterimApprovalNLSMergeTemplate]"

                    objWord.ActiveDocument.MailMerge.Execute
    End If
End Sub


Just replace the bits like table name with your details.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.