Hi all,
I have the code that runs the reports and put them as html into email's body. What I should write if I want the report to skip on sending the email if report is empty? Let's say the report in Mail 2 - Barbara is empty, I want the first one to be displayed and third one and the middle one to be skipped. The same situation could be with mail 1 and mail 2. I tried many macros but all of them stopped the action and did not go to Mail 3.
Private Sub Combo_mail_Click()
Dim sFile As String, lFile As Long, sHtml As String
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
'Mail 1 - Output the report to HTML in the temp directory
sFile = Environ$("HOMEPATH") & "\Documents" & ".html"
DoCmd.OutputTo acOutputReport, "Report_John", acFormatHTML, sFile
'Read in the HTML File
lFile = FreeFile
Open sFile For Input As lFile
sHtml = Input$(LOF(lFile) - 1, lFile)
Close lFile
'Put the file contents in the email body
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = "john@email.com"
olMail.CC = "mark@email.com"
olMail.Subject = "Report"
olMail.HTMLBody = sHtml
olMail.Display
'Mail 2 - Output the report to HTML in the temp directory
sFile = Environ$("HOMEPATH") & "\Documents" & ".html"
DoCmd.OutputTo acOutputReport, "Report_Barbara", acFormatHTML, sFile
'Read in the HTML File
lFile = FreeFile
Open sFile For Input As lFile
sHtml = Input$(LOF(lFile) - 1, lFile)
Close lFile
'Put the file contents in the email body
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = "barbara@email.com"
olMail.CC = "mark@email.com"
olMail.Subject = "Report"
olMail.HTMLBody = sHtml
olMail.Display
'Mail 3 - Output the report to HTML in the temp directory
sFile = Environ$("HOMEPATH") & "\Documents" & ".html"
DoCmd.OutputTo acOutputReport, "Report_Andre", acFormatHTML, sFile
'Read in the HTML File
lFile = FreeFile
Open sFile For Input As lFile
sHtml = Input$(LOF(lFile) - 1, lFile)
Close lFile
'Put the file contents in the email body
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = "andre@email.com"
olMail.CC = "mark@email.com"
olMail.Subject = "Report"
olMail.HTMLBody = sHtml
olMail.Display
End
End Sub
Thanks,
Tom