Possible to NOT have to open reports when sending them as an attachment in an email

Am wondering if the following code could be altered so that the reports don't have to open prior to sending them as attachments in an email.  If so, how?

    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Dim fileNameIncomeStatement As String, todayDateIncomeStatement As String
    Dim fileNameSalesAnalysis As String, todayDateSalesAnalysis As String
    Dim rptIncomeStatement As Object
    Dim rptSalesAnalysis As Object
    Dim FileArray As String
    Dim AttachmentFiles, aFile
    todayDateIncomeStatement = Format(Date, "MMDDYYYY")
    todayDateSalesAnalysis = Format(Date, "MMDDYYYY")
    'Email Reports...
    fileNameIncomeStatement = Application.CurrentProject.Path & "\Income Statement_" & todayDateIncomeStatement & ".pdf"
    DoCmd.OutputTo acReport, "Income Statement", acFormatPDF, fileNameIncomeStatement, , , , acExportQualityPrint

    DoCmd.OpenReport "Income Statement", acViewPreview, , , acHidden
    Reports![Income Statement].Visible = False

    fileNameSalesAnalysis = Application.CurrentProject.Path & "\Sales Analysis_" & todayDateSalesAnalysis & ".pdf"
    DoCmd.OutputTo acReport, "Sales Analysis", acFormatPDF, fileNameSalesAnalysis, , , , acExportQualityPrint

    DoCmd.OpenReport "Sales Analysis", acViewPreview, , , acHidden
    Reports![Sales Analysis].Visible = False

    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)

    With MailOutLook
        .Recipients.Add Reports![Income Statement].txtEMail
        .CC = "acct@somedomain.com"
        .Subject = "Financial Statement for " & Reports![Income Statement].txtStartDate & " - " & Reports![Income Statement].txtEndDate
        .Body = "Attached are both your Income Statement and Sales Analysis.  If you have any questions please contact Susanne at someemail@somedomain.com or 123 456 7890."
        .Attachments.Add fileNameSalesAnalysis
        .Attachments.Add fileNameIncomeStatement
    End With

    DoCmd.Close acReport, "Income Statement", acSaveNo
    DoCmd.Close acReport, "Sales Analysis", acSaveNo
    'End of email Reports

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Seems like you should just be able to remove these lines, since you have already generated the attachment files before opening the reports.

    DoCmd.OpenReport "Income Statement", acViewPreview, , , acHidden
    Reports![Income Statement].Visible = False

    DoCmd.OpenReport "Sales Analysis", acViewPreview, , , acHidden
    Reports![Sales Analysis].Visible = False

    DoCmd.Close acReport, "Income Statement", acSaveNo
    DoCmd.Close acReport, "Sales Analysis", acSaveNo

Open in new window

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
hi Steve,

adding on to Bill's comment ...
Open (and possibly Save and Close) a report before OutputTo to, for instance, send Where parameter for OpenReport, or set RecordSource, or Filter. I don't see that happening here.  Therefore, in addition to what Bill told you, the Close statements (ie: DoCmd.Close acReport, "In...) can go away, as can DIMming the variables (Dim rptIncomeStatement As Object) for report objects.

And, of course, another reason to open a report is for someone to look at it ... but that doesn't seem to be happening either


You can Sleep between generating PDFs and sending email to make sure the files are done.


to the top of the module, be sure to add this if it is not there so when you compile, more errors will be picked up
Option Explicit

Open in new window


for the email to go automatically ... switch to comment Display and uncomment Send:

Open in new window

have an awesome day,
.Recipients.Add Reports![Income Statement].txtEMail
This statement also needs to be changed.  It is referring to an open report.  You need to get the email address some other way.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
good catch, Pat

Steve, I did not notice that report controls were referred to in the email construction -- it is done in Subject too.  

I also see that private information in your original question needs to be obfuscated ... have asked a moderator for help.

as it is now, you do have to open the reports. As Pat said, that information could be gotten differently ... if there a form to collect criteria that drives the reports? Where is the recipient email address stored?

if this information can be gotten without opening the reports, that would be better ~

btw, the example shows what appears to be bad data structure ... seemingly, instead of one field for notes (with many note records for different times), there appears to be many fields in the same record with names having numbers (ie: Time_1, Time_2, ...)  ~~ this is an opportunity for problems
SteveL13Author Commented:
I'm confused...  what personal data?
SteveL13Author Commented:
Oh, I see.  Thank you.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
thanks, South Mod ~

Steve, to summarize, you can avoid opening the reports* if you can use another way to supply the needed data that currently comes from the [Income Statement] report -- txtEMail, txtStartDate, txtEndDate -- perhaps using  ... information from controls on form if there is one, dLookup to get data from a table or query, open recordset to get what you want, etc, ...

* [Income Statement] seems to be the only report referenced -- so you should be able to eliminate opening the other one, [Sales Analysis] -- including DIM its object variable, OpenReport, Closing it, and releasing it (which is not done here anyway -- however, rptSalesAnalysis is not actually assigned so it could be eliminated, as can rptIncomeStatement), without any harm as the code is now.  FileArray and AttachmentFiles are not used either

btw, this:
Reports!MyReportName.Visible = False
is not necessary for an object that is hidden

and, please, if you have control over the data structure**, consider normalizing the data ... while a bigger step if you already built forms and reports, this will save you many headaches down the road, give you more flexibility**, and better performance.

**referencing my previous comment, "fields in the same record with names having numbers (ie: Time_1, Time_2, ...) " -- If data was normalized, you could find or filter for a word or phrase, or date, for all comments without having to concatenate or search a bunch of fields

have an awesome day,
Helen FeddemaCommented:
There is no need to open the reports -- just save as PDFs and attach the PDF file(s) to the email:

Private Function SendPDFReport() 
'Created by Helen Feddema 17-Jan-2010 
'Last modified 17-Jan-2010 
On Error GoTo ErrorHandler 
   Dim appOutlook As New Outlook.Application 
   Dim itm As Outlook.MailItem 
   Dim strFileName As String 
   Dim rpt As Access.Report 
   Dim strReport As String 
   Dim strCurrentPath As String 
   Dim strFileNameAndPath As String 
   strCurrentPath = Application.CurrentProject.Path & "\" 
   strReport = "rptProductPrices" 
   strFileName = "Product Prices.pdf" 
   strFileNameAndPath = strCurrentPath & strFileName 
   'Output report to PDF in current path 
   DoCmd.OutputTo objecttype:=acOutputReport, _ 
      objectname:=strReport, _ 
      outputformat:=acformatpdf, _ 
      outputfile:=strFileNameAndPath, _ 
   'Create new mail message and attach file to it 
   Set itm = appOutlook.CreateItem(olMailItem) 
   With itm 
      .To = "someone@xyz.com" 
      .Subject = "Daily report" 
      .Body = "Your message" 
      .Attachments.Add strFileNameAndPath 
      'To edit before sending 
      'To send automatically 
   End With 
   Exit Sub 
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description 
   Resume ErrorHandlerExit 
End Function

Open in new window

You can put the date info into the PDF names if needed.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.