Originally Posted by
niki09
...I do not want the template report emailed with the template report ame, but with the name of the data it shows....
I do not know that I fully understand what the objective is or why you sometimes get an error. Are you trying to provide the attachment file a custom name? I think I remember a solution for this posted somewhere. I can't seem to find it right now. I believe it used Outlook automation to rename the attachment.
What I do is use Docmd.OutputTo to save the report as a PDF. Then I use automation to attach the PDF. The problem with this is you have to create extra code to make sure there is not an existing PDF file in your temp directory and that a temp directory actually exists.
Code:
'before exporting your report you will want to make
'sure there is not a file in the folder with the same name
'and that the folder actually exists
'create a report in a temp folder
DoCmd.OutputTo acOutputReport, "ReportName", acFormatPDF, "C:\Test\PDF_Files\Test.pdf"
'Reference Microsoft Outlook and
'use early binding
Dim objOutlook As New Outlook.Application
Dim objNewEmail As MailItem
Dim objAttachReport As Attachments
'Instantiate your objects
Set objNewEmail = objOutlook.CreateItem(olMailItem)
Set objAttachReport = objNewEmail.Attachments
'Add your file to the Attachments Collection
objAttachReport.Add "C:\Test\PDF_Files\Test.pdf", olByValue
With objNewEmail
.BodyFormat = olFormatRichText
.To = "MyEmail@Domain.com"
.Subject = "Test Message"
.HTMLBody = "This is the body of the message"
'Save the email to ensure the attachment is a
'copy of the original file (olByValue)
.Save
.Send
End With
'tidy up
Set objAttachReport = Nothing
Set objNewEmail = Nothing
Set objOutlook = Nothing
MsgBox "Complete"