Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Looping email with personalized attachment - Two issues

Posted on 2015-01-22
3
Medium Priority
?
170 Views
Last Modified: 2015-01-27
I'm using VBA code to loop through 'TblEmailAddresses' which is supposed to send a report to each user in the table.  The first step (I haven't yet gotten to the attachment really) is to send the email to each user.  The code below actually outputs a singular report with each user's name as a suffix.  It's ironic that the report name loops (as the attachment suffix) but the '.To' doesn't loop.  That is
      goal #1 - send multiple emails to multiple users.  (It is sending multiple emails but all to the same user.)
      goal #2 - attach a singular report with underlying query personalized for the looped user.  So that's really the same report with different data therein.

Please review the code and help me to loop .To correctly.  If you have time and think it appropriate (extra credits :)), please provide ideas of how to make one report work for each user.  Do I need multiple reports?  There are only nine users but I figure there must be something more streamlined with the use of code.  

If I should ask #2 in a separate question, that's okay too.  Thanks.

Dim MailOutLook As Outlook.MailItem
     Set appOutLook = CreateObject("Outlook.Application")
     Set MailOutLook = appOutLook.CreateItem(olMailItem)
Dim ContactEM As String
Dim SourceTable As String
    Set db = CurrentDb
    Set rstTables = db.OpenRecordset("TblEmailAddresses")

Do While Not rstTables.EOF
FileName = "H:\UnApprovedDetailedHours_Signoff_" & rstTables!Contact & ".pdf"
DoCmd.OutputTo acOutputReport, "RptEmployeeUnApprovedHrsEmail_HRSignoff", acFormatPDF, "H:\UnApprovedDetailedHours_Signoff.pdf", False, ""

Name "H:\UnApprovedDetailedHours_Signoff.pdf " As FileName  'Name "C:\WINDOWS\UnApprovedDetailedHours_Signoff.pdf " As FileName
            Set appOutLook = CreateObject("Outlook.Application")
            Set MailOutLook = appOutLook.CreateItem(olMailItem)
            With MailOutLook
            .BodyFormat = olFormatHTML
            .To = DLookup("EmailAddress", "TblEmailAddresses", rstTables!Contact)
            .Subject = "Unapproved Pay Hours Signoff"
            .HTMLBody = "Attached is a report that provides details of unapproved but paid hours for the current pay. " & _
            "Please sign, scan and return the report to Payroll@carefirst.com " & _
            "If you have any questions about this report, please call Bill Smith (111-222-7777)." & "<BR>" & "<BR>" & "<BR>" & "<BR>" & _
            "Thanks," & "<BR>" & "<BR>" & _
            "Bill Smith" & "<BR>" & _
            "(111) 222-7777"
            .Attachments.Add "H:\UnApprovedDetailedHours_Signoff_" & rstTables!Contact & ".pdf"
            .Close 0 '0 = olSave
            End With
            rstTables.MoveNext
        Set MailOutLook = Nothing
        Kill ("H:\UnApprovedDetailedHours*.pdf")
    Loop
0
Comment
Question by:CFMI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40565598
Can you simplify your question here? (perhaps using something simple like Customers and orders)
I have_________
I need to send____________, to___________

Is your goal here to send a few custom PDF's (filtered for that particular person), to each individual person?
for example: each user will get 1 email with 3 PDf's
...Then you can tr something like the attached file:
Access-EEQ27816383EmailMultipleIndividua
0
 
LVL 20

Accepted Solution

by:
GrahamMandeno earned 2000 total points
ID: 40565791
Hi Bill (?)

Your main problem with #1 is in this line:
            .To = DLookup("EmailAddress", "TblEmailAddresses", rstTables!Contact)

The third argument of DLookup should be a valid expression for a SQL WHERE clause.  This would be something like:
          "Contact='" & rstTables!Contact & "'"

However, you don't need DLookup at all, because you already have TblEmailAddresses open, and positioned on the desired record!  All you need is this:
            .To = rstTables!EmailAddress

#2 is going to present you with a problem, because the OutputTo method does not allow for a filter or WHERE condition on the report.  Therefore, ALL the contacts will get the full report showing ALL the Unapproved Pay Hours Signoff information for ALL the other contacts as well as their own.

The trick is to open the report hidden, with only the information pertaining to the current contact, and then use OutputTo to export it as a PDF:

FileName = "H:\UnApprovedDetailedHours_Signoff_" & rstTables!Contact & ".pdf"
DoCmd.OpenReport "RptEmployeeUnApprovedHrsEmail_HRSignoff", acViewPreview, , _
      "ContactID=" rstTables!ContactID, acHidden
DoCmd.OutputTo acOutputReport, "RptEmployeeUnApprovedHrsEmail_HRSignoff", _
       acFormatPDF, FileName, False, ""

Open in new window


A few other comments on your code:
You are setting both appOutlook and MailOutlook both inside and outside the loop.  appOutlook needs to be set only once, outside the loop, and MailOutlook needs to be set for each message, inside the loop.
there is no need to export the PDF as a "Standard" name and then rename the file - just export it as the file with the contact name as above
I understand you are testing at the moment, but when you're happy with the code you will probably want to replace .Close 0 with .Send
Be sure to close rstTables and set appOutlook to Nothing before you exit the procedure

Good luck!
Graham Mandeno [Access MVP 1996-2015]
0
 
LVL 1

Author Closing Comment

by:CFMI
ID: 40567027
You guys are the best!   After changing  .To = rstTables!EmailAddress, #1 was resolved.  Then I added a table to be used for the report.  I looped through a 'strSql' which added only the corresponding data for each iteration.  So the code looked like this:

Do While Not rstTables.EOF
DoCmd.OpenQuery "qDel_tblCF_EmpUnapprHrs_Signoff", acViewNormal, acEdit

     strSql = "INSERT INTO tblCF_EmpUnapprHrs_Signoff ( [Fields])" & _
        "SELECT [Fields]" & _
        "From [Tables]" & _
        "WHERE ((([Field])= " & Chr(34) & rstTables!SeniorVP & Chr(34) & ") AND ((rstTables!.[Field])<>'Y'));"

FileName = "H:\UnApprovedDetailedHours_Signoff_" & rstTables!Contact & ".pdf"
DoCmd.RunSQL strSql
Debug.Print

DoCmd.OutputTo acOutputReport, "RptEmployeeUnApprovedHrsEmail_HRSignoff", acFormatPDF, "H:\UnApprovedDetailedHours_Signoff.pdf", False, ""

Name "H:\UnApprovedDetailedHours_Signoff.pdf " As FileName  'Name "C:\WINDOWS\UnApprovedDetailedHours_Signoff.pdf " As FileName
            Set appOutLook = CreateObject("Outlook.Application")
            Set MailOutLook = appOutLook.CreateItem(olMailItem)
            With MailOutLook
            .BodyFormat = olFormatHTML
            .To = rstTables!EmailAddress
            .Subject = "Unapproved Pay Hours Signoff"
            .HTMLBody = "Attached is a report that provides details of unapproved but paid hours for the current pay. " & _
            "Please sign, scan and return the report to Payroll@carefirst.com " & _
            "If you have any questions about this report, please call Bill (111-222-7777)." & "<BR>" & "<BR>" & "<BR>" & "<BR>" & _
            "Thanks," & "<BR>" & "<BR>" & _
            "Bill " & "<BR>" & _
            "(111) 222-7777"
            .Attachments.Add "H:\UnApprovedDetailedHours_Signoff_" & rstTables!Contact & ".pdf"
            .Close 0 '0 = olSave
            End With
            rstTables.MoveNext
        Set MailOutLook = Nothing
        Kill ("H:\UnApprovedDetailedHours*.pdf")
    Loop
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question