• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 51
  • Last Modified:

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

0
SteveL13
Asked:
SteveL13
4 Solutions
 
Bill PrewCommented:
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


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

Open in new window

have an awesome day,
crystal
0
 
PatHartmanCommented:
.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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
0
 
SteveL13Author Commented:
I'm confused...  what personal data?
0
 
SteveL13Author Commented:
Oh, I see.  Thank you.
0
 
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,
crystal
0
 
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, _ 
      autostart:=False 
    
   '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 
      .Display 
      'To send automatically 
      '.Send 
   End With 
    
ErrorHandlerExit: 
   Exit Sub 
 
ErrorHandler: 
   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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now