Here you go:
Thanks for your invaluable help.
Code:
Private Sub cmdEmail_Click()
Dim rs As DAO.Recordset
Dim strSQL As String
Dim rpt As Report
Dim strFile As String
Dim strPath As String
Dim strReport As String
Dim strFullPath As String
DoCmd.OpenReport "rptRealGrouped", acViewPreview, , , acHidden
Set rpt = Reports("rptRealGrouped")
strReport = CStr(rpt.Name)
Select Case Me.OpenArgs
Case "Print All"
strSQL = "SELECT DISTINCT Temp.ID, Temp.Email, Temp.Name " _
& "FROM (" & Replace(rpt.RecordSource, ";", "") & ")as Temp" _
& " WHERE (((Temp.Email) Is Not Null And ([Temp].[Email])<>"""") AND (([Temp].[Name]) Is Not Null And ([Temp].[Name])<>""""))"
Case "Print Selected"
strSQL = "SELECT DISTINCT Temp.ID, Temp.Email, Temp.Name " _
& "FROM (" & Replace(rpt.RecordSource, ";", "") & ")as Temp" _
& " WHERE (((Temp.Email) Is Not Null And ([Temp].[Email])<>"""") AND (([Temp].[Name]) Is Not Null And ([Temp].[Name])<>"""")) And " _
& Replace(gWhere, "B.ID", "Temp.ID")
Case "AllUpdates"
strSQL = "SELECT DISTINCT Temp.ID, Temp.Email, Temp.Name, Temp.DateUpdated " _
& "FROM (" & Replace(rpt.RecordSource, ";", "") & ") as Temp" _
& " WHERE (([Temp].[Email]) Is Not Null And ([Temp].[Email])<>"""") AND (([Temp].[Name]) Is Not Null And ([Temp].[Name])<>"""")"
strSQL = strSQL & "[Temp].[DateUpdated] Between " & [Forms]![frmDateRange]![txtFrom] & " And " & [Forms]![frmDateRange]![txtTo]
Case "SelectedUpdates"
strSQL = "SELECT DISTINCT Temp.ID, Temp.Email, Temp.Name, A.DateUpdated " _
& "FROM (" & Replace(rpt.RecordSource, ";", "") & ")as Temp, tblApartments as A" _
& " (((Temp.Email) Is Not Null And ([Temp].[Email])<>"""") AND (([Temp].[Name]) Is Not Null And ([Temp].[Name])<>"""")) " _
& "WHERE " & Replace(gWhere, "B.ID", "Temp.ID")
Case Else
strSQL = ""
End Select
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)
While Not rs.EOF
rpt.Filter = "[B].[ID] = " & rs!ID
rpt.FilterOn = True
' Set name of file
strFile = Format(Date, "mm.dd.yyyy") & " - " & rs!Name & ".pdf"
' Set path of file
strPath = GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\MyTemp\"
' Open the report in hidden state so it applies all settings (vba) set on report events
DoCmd.OpenReport strReport, acViewPreview, , , , acHidden
' For debuging purposes
Debug.Print "Name: " & rs!Name & " " & "Email: " & rs!Email
' Save to pdf
' DoCmd.OpenReport "rptRealGrouped", acViewPreview ', , , , acHidden
' The next line will only work in Access 2010 (or 2007 if Microsoft Pdf Addin was installed)
DoCmd.OutputTo acOutputReport, "rptRealGrouped", acFormatPDF, strPath & strFile
' ' Comment the previous line and uncomment the next for backwards compatibility
' ' i.e. Acccess 2007 (without microsoft pdf addin)or earlier
' 'Call PrintReportAsPDFwithBullZip(strReport, , strPath, strFile)
' DoCmd.Close acReport, "rptRealGrouped", acSaveNo
' Set full path (including name) of saved pdf
strFullPath = strPath & strFile
''' ' Send mail
''' Call SendEmail(rs!Email, "", , , , , , , , , , , , , strFullPath)
'delete the pdf
Kill strPath & strFile
rs.MoveNext
Wend
'Cleanup
rs.Close
Set rs = Nothing
' DoCmd.Close acForm, Me.Name
End Sub
Thanks for your invaluable help.