Option Compare Database Option Explicit Private strStartHTML As String Public strRptName As String ' Oliver Stohr, Access MVP has graciously given us permission ' to make his code available. I also included his comments ' from his blog. ' By Oliver Stohr, Access MVP ' http://www.access-freak.com/blog.html#BlogEntryAugust200802 ' Modified by Patrick Wood ' http://gainingaccess.net ' I added a couple of other Procedures ' that can be helpful with Emailing Reports ' I added the basFileExists and the IsAppThere ' Functions so the code will work for everyone ' The Report Name must be a minimum of 7 characters to identify the files ' varPath is a Folder Path where the outputed files are saved. It must ' already exist and the trailing slash must be included. ' To maintain spaces between Records on my Report I added a bound textbox ' and made the forecolor white, effectively making it invisible ' This could also be done with the Report's Footer so you will not see ' the page numbers every few lines ' you can test it in the Immediate Window ' Example: ' Call RptEmailHTML("C:\", "rptApptCalPortrait") '--------------------------------------------------------------------------------------- ' Procedure : RptEmailHtml ' DateTime : 9/15/2008 13:57 ' Author : Patrick Wood & Unknown ' Purpose : Send a Report as HTML in the body of an Email ' Arguments : strRptName the name of the Report you want to send ' : varPath is a Folder Path that must exist--must include slash at end '--------------------------------------------------------------------------------------- ' Public Function RptEmailHtml(varPath As Variant, strRptName As String) On Error GoTo Err_RptEmailHtml Dim HTMLFile As String 'HTML name to hold formatting of Report Dim i As Integer Dim strBody As String Dim strTemp As String Dim strflLeft As String Dim strFirstFile As String Dim arrFiles ' We need a way to identify the Exported Report HTML files If Len(strRptName & vbNullString) < 7 Then Call MsgBox("The Report Name needs to be at least 7 characters long " _ & vbCrLf & " so the files being used can be verified." _ , vbExclamation, " Please Make Your Report Name Longer") GoTo Exit_RptEmailHtml End If ' Set a name for the first HTML file HTMLFile = varPath & strRptName & ".HTML" ' Use the first file's path and name to get ' the HEAD HTML in the ReadHTMLFile Sub strFirstFile = HTMLFile ' Create the File(s) ' Creates one file for each Report Page DoCmd.OutputTo acOutputReport, strRptName, acFormatHTML, _ HTMLFile, False ' Get a value to identify the Files strflLeft = Left(strRptName, 7) arrFiles = Array(dir(varPath, vbNormal)) ' Retrieve the first entry. Do While arrFiles(i) <> 0 ' Start the loop. If strflLeft = Left(arrFiles(i), 7) Then strTemp = ReadHTMLFile(varPath & arrFiles(i), strFirstFile) strBody = strBody & "
" & strTemp Debug.Print varPath & arrFiles(i) End If ' Debug.Print varPath & arrFiles(i) ' An Expected Error occurs when there are no more files On Error GoTo StepOut ' Get out of the Loop arrFiles(i) = dir ' Get next entry. Loop StepOut: ' Clear the Error Err.Number = 0 ' Add the start of the HTML to the BODY strBody = strStartHTML & strBody ' We have gathered all the HTML from all the files so make the email. SendHTMLMail (strBody) Exit_RptEmailHtml: Exit Function Err_RptEmailHtml: Call MsgBox(Err.Description & vbCrLf & "Error Number: " & Err.Number & vbCrLf & _ " In procedure RptEmailHtml of Module basEmail") Resume Exit_RptEmailHtml End Function Sub SendHTMLMail(strBodyText) Dim strTo As String 'recipiants Dim olApp As Object ' New Outlook.Application Dim olMail As Object ' Outlook.MailItem Dim strSubject As String 'Email subject Dim strMsg As String 'Email greeting If isAppThere("Outlook.Application") = False Then ' Outlook is not open, create a new instance Set olApp = CreateObject("Outlook.Application") Else ' Outlook is already open--use this method Set olApp = GetObject(, "Outlook.Application") End If ' Create the New Email Item Set olMail = olApp.CreateItem(0) ' olMailitem = 0 strMsg = "Here is the Report I Promised." strSubject = "My Report HTML in Email Body Test" With olMail .To = "joe@irjoe.com" .Subject = "My Report HTML in Email Body Test" '.BodyFormat = olFormatRichText .HTMLBody = strMsg & strBodyText & "