I have the following code to save a report as a pdf, email it as an attachment and then delete the pdf. It works fine.
I don't want the email to send if the report does not contain data though, so I included this in the code.
Checking the report for data however does not work because it says the report is not open. I think because the code runs too fast for the report to finish loading. Any ideas on how the accomplish this?
Code:
Dim iCfg As Object
Dim iMsg As Object
Set iCfg = CreateObject("CDO.Configuration")
Set iMsg = CreateObject("CDO.Message")
With iCfg.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "exc2007.futuraind.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Update
End With
'Stop
With iMsg
.Configuration = iCfg
.Subject = "Late Orders"
stremail = DLookup("email", "reminder_date")
.to = stremail
.TextBody = "Attached is a list of items that have not been ordered yet. Open the TR Card database for more information."
.AddAttachment "G:\DATA\FUTURA\FINISHG\Titrations_TRcards\TR Cards\TR Master Database\unpurchased orders.pdf"
.from = ""
.sender = ""
.send
End With
Set iMsg = Nothing
Set iCfg = Nothing
Kill "G:\DATA\FUTURA\FINISHG\Titrations_TRcards\TR Cards\TR Master Database\unpurchased orders.pdf"
DoCmd.SetWarnings False
DoCmd.OpenQuery "reminder_date query"
DoCmd.SetWarnings True
Code:
DoCmd.OpenReport "unpurchased orders" If Reports![unpurchased orders].HasData = True Then 'run the email code from above else exit sub end if
Comment