Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

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
        .Display
        '.Send
    End With

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

Open in new window

SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

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:
        '.Display
        .Send

Open in new window

have an awesome day,
crystal
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of SteveL13

ASKER

I'm confused...  what personal data?
Oh, I see.  Thank you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial