Dim objOutlook As Outlook.Application Dim objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient Dim strReportName As String Dim strReportPath As String Dim strReportFullPath As String Dim stDocName As String Dim strMyString As String Dim strReportText As String Dim strTo As String ' Create the Outlook session. Set objOutlook = CreateObject("Outlook.Application") ' Create the message. Set objOutlookMsg = objOutlook.CreateItem(olMailItem) With objOutlookMsg ' Add the To recipient(s) to the message. Substitute ' your names here. strTo = "TO details" Set objOutlookRecip = .Recipients.Add(strTo) objOutlookRecip.Type = olTo ' Add the CC recipient(s) to the message. strCC = "CC details" Set objOutlookRecip = .Recipients.Add(strCC) objOutlookRecip.Type = olCC ' Add the Subject to the message. strSubject = "Subject Text" .Subject = (strSubject) 'Set the variables strReportPath = "Path to save report" strReportName = "Report Name" strReportFullPath = strReportPath & strReportName 'Open the report, hidden an filtered to the current form record DoCmd.OpenReport strReportName, acViewPreview, , "id=" & Me.id, acHidden 'Output the report as HTML DoCmd.OutputTo acOutputReport, strReportName, acFormatHTML, strReportFullPath & ".html" 'Close the Report DoCmd.Close acReport, strReportName '--- Create a single line text file from the HTML Report, '--- in order to be displayed propely as an HTML Document in the Email body '--- Code modified from here: '--- http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_21294504.html#a13177554 'Open file for input (modification) Open strReportFullPath & ".HTML" For Input As #1 Do While Not EOF(1) 'Read each Line into strMyString variable. Line Input #1, strMyString strReportText = strReportText & strMyString 'Put all the Lines read into one String, FullText Loop Close #1 'Insert the Full Report (as a simgle Line string) into the email body .HTMLBody = strReportText ' Add the attachment to the message stDocName = "Store Logins" DoCmd.SendObject acReport, stDocName, acFormatPDF ' Resolve each Recipient's name. For Each objOutlookRecip In .Recipients If Not objOutlookRecip.Resolve Then objOutlookMsg.Display End If Next 'Send email without viewing it. '.Save '.Send 'Dispay email before sending. .Display End With 'Cleanup Code Set objOutlookMsg = Nothing Set objOutlook = Nothing Set objOutlookRecip = Nothing End Sub
From novice to tech pro — start learning today.