Private Sub cmdEMailReports_Click()
On Error GoTo Error_Handler
If Me.Dirty Then Me.Dirty = False
Dim OutApp As Object
Dim OutMail As Object
Dim rstClone As DAO.Recordset
Dim strWhere As String
Dim strEmail As String
Dim strMsg As String
Dim strSubject As String
'Renewal
Const strSubjectRenewal As String = "Renewal Attached"
Dim strMsgRenewal As String
Const sReportNameRenewal As String = "Renewal Details Gift Aid & SO"
Dim strFileNameRenewal As String
'Gift Aid
Const strSubjectGiftAid As String = "Gift Aid and Renewal Attached"
Dim strMsgGiftAid As String
Const sReportNameGiftAid As String = "rptGiftAid"
Dim strFileNameGiftAid As String
'General constants
Const olMailItem = 0
Const olFormatHTML = 2
strMsgRenewal = "<!DOCTYPE html>" & _
"<html lang='en'>" & _
"<head>" & _
"<meta charset='UTF-8'>" & _
"<style>" & _
"p {font-size: 11pt; font-family: Calibri;}" & _
"</style>" & _
"</head>" & _
"<body>" & _
"<p>Attached are your Renewal details.</p>" & _
"<p>Any queries please let me know.</p>" & _
"<p>Thank You</p>" & _
"</body>" & _
"</html>"
strMsgGiftAid = "<!DOCTYPE html>" & _
"<html lang='en'>" & _
"<head>" & _
"<meta charset='UTF-8'>" & _
"<style>" & _
"p {font-size: 11pt; font-family: Calibri;}" & _
"</style>" & _
"</head>" & _
"<body>" & _
"<p>Attached are your Renewal and Gift Aid Forms.</p>" & _
"<p>Any queries please let me know.</p>" & _
"<p>Thank You</p>" & _
"</body>" & _
"</html>"
Set rstClone = Me.RecordsetClone
If rstClone.RecordCount = 0 Then
MsgBox "No records for report!"
Exit Sub
Else
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
rstClone.MoveFirst
While Not rstClone.EOF
strFileNameRenewal = ""
strFileNameGiftAid = ""
strEmail = rstClone!Email 'To e-mail address
strWhere = "ContactID = " & rstClone!ContactID 'Report WHERE
'Renewal Report Included For All Cases
strFileNameRenewal = "C:\Emails\" & Format(Date, "mmddyyyy") & "- Renewals -" & rstClone!MemberNo & ".pdf"
Call GeneratePDFofReport(sReportNameRenewal, strFileNameRenewal, strWhere)
If rstClone![Gift Aid] = 0 Then
strSubject = strSubjectGiftAid
strMsg = strMsgGiftAid
'Gift Aid Report Only Included If Selected
strFileNameGiftAid = "C:\Emails\" & Format(Date, "mmddyyyy") & "- Gift Aid -" & rstClone!MemberNo & ".pdf"
Call GeneratePDFofReport(sReportNameGiftAid, strFileNameGiftAid, strWhere)
Else
strSubject = strSubjectRenewal
strMsg = strMsgRenewal
End If
' OUTLOOK Sending Bits
Set OutMail = OutApp.CreateItem(olMailItem)
On Error Resume Next
With OutMail
.To = strEmail
.Subject = strSubject
.BodyFormat = olFormatHTML
.HTMLBody = strMsg
.ReadReceiptRequested = False
If strFileNameRenewal <> "" Then .Attachments.Add strFileNameRenewal
If strFileNameGiftAid <> "" Then .Attachments.Add strFileNameGiftAid
.Display 'or use .Send to immediately send. As this runs with .Display it will save the emails in your drafts.
End With
On Error GoTo Error_Handler
Set OutMail = Nothing
rstClone.MoveNext
Wend
On Error GoTo 0
End If
Error_Handler_Exit:
On Error Resume Next
Set OutMail = Nothing
Set OutApp = Nothing
Set rstClone = Nothing
Exit Sub
Error_Handler:
If Err.Number <> 2501 Then
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: cmdEMailReports_Click" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End If
End Sub
Private Sub GeneratePDFofReport(ByVal sReportName As String, _
ByVal sPDFFileName As String, _
Optional ByVal sReportWHERE As String)
On Error GoTo Error_Handler
DoCmd.OpenReport sReportName, acViewPreview, , sReportWHERE, acHidden ' acWindowNormal '- No need to make it display to the user!
DoCmd.OutputTo acOutputReport, sReportName, acFormatPDF, sPDFFileName, , , , acExportQualityPrint
DoEvents
DoCmd.Close acReport, sReportName
Error_Handler_Exit:
On Error Resume Next
Exit Sub
Error_Handler:
If Err.Number <> 2501 Then
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: GeneratePDFofReport" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End If
End Sub