473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Email multiple contacts and attach a report specific to each contact

bre1603
39
I have a continuous form in Access 2007 that lists contacts for different agencies. Each record has a checkbox that I can use to generate a mass email to all selected contacts – it’s generic with no subject, no attachments and no message, for everyday use. However, the database owners would also like the ability to email a report to each contact (asking them to verify and update their information) using the checkboxes on this form.

Ideally, they would select the contacts they wanted to email, click the “Email Info Update Form” button and have Outlook send out an email to each of the contacts with their Contact Info Report attached. (I can address whether the emails go automatically or get reviewed later – I’m really just interested in getting the report attached to each email.)

Here's what I have-

This code (it's rough) opens multiple emails, although there seems to be a limit to the quantity. But I can' get the report to attach to more than one record-the one that has the focus at the time the button is clicked (that's why it's commented out):
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command18_Click()
  2.  
  3. Dim ol As Object
  4. Dim msg As Object
  5. Dim rec As DAO.Recordset
  6. Dim intCount As Integer
  7. Dim intLoop As Integer
  8.  
  9. Set ol = CreateObject("Outlook.Application")
  10.  
  11. Set rec = CurrentDb.OpenRecordset("qryLeadershipEmailListALL SelectQuery")
  12. If Not (rec.BOF And rec.EOF) Then
  13. rec.MoveLast
  14. rec.MoveFirst
  15. intCount = rec.RecordCount
  16. For intLoop = 1 To intCount
  17. Set msg = ol.CreateItem(olMailItem)
  18. msg.Subject = "ENTER YOUR SUBJECT HERE"
  19. msg.To = rec("Leadership Email")
  20. msg.Body = "Message goes here."
  21. msg.Body = msg.Body & Chr(13) & Chr(13) & "It's a really long message."
  22.  
  23. 'DoCmd.OpenReport "AgencyUpdateForm", acViewPreview, , "[Agency Number]= forms![LeadershipEmailListALL].[Agency Number]"
  24. 'DoCmd.SendObject acSendReport, "AgencyUpdateForm", acFormatPDF, msg.To, , , msg.Subject, msg.Body
  25.  
  26. 'msg.Attachments.Add "AgencyUpdateForm"
  27. ', acViewPreview, , "[Agency Number]= forms![LeadershipEmailListALL].[Agency Number]"
  28. msg.Display
  29.  
  30. Set msg = Nothing
  31. rec.MoveNext
  32. Next intLoop
  33. End If
  34.  
  35. Set rec = Nothing
  36. Set ol = Nothing
  37.  
  38. End Sub

This code attaches the report but only opens an email for the first contact:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command43_Click()
  2. On Error GoTo Err_Command43_Click
  3.  
  4. Me.Refresh
  5.  
  6. Dim strEMail As String
  7. Dim strAddr As String
  8. Dim dbs As DAO.Database
  9. Dim rstEMail As DAO.Recordset
  10. Dim stTo As String
  11. Dim stCC As String
  12. Dim stBcc As String
  13. Dim stMessageSummary As String
  14. Dim stSubjectSummary As String
  15.  
  16. 'Retrieve all E-Mail Addressess
  17. Set dbs = CurrentDb
  18. Set rstEMail = dbs.OpenRecordset("qryLeadershipEmailListALL SelectQuery")
  19.  
  20. With rstEMail
  21.   Do While Not .EOF
  22.     'Build the Recipients String
  23.     strEMail = strEMail & ![Leadership Email] & ";"
  24.       .MoveNext
  25.   Loop
  26. End With
  27.  
  28.  
  29. stSubjectSummary = "Agency Contact Update Request for " & Me.Agency_Name
  30.  
  31.     stMessageSummary = "Message goes here." & ""
  32.     stMessageSummary = stMessageSummary & Chr(13) & Chr(13) & "It's a really long message"
  33.  
  34. 'Determine the stTo line
  35.  
  36.     stTo = [Leadership Email]
  37.     stCC = ""
  38.     stBcc = ""
  39.  
  40. rstEMail.Close
  41. Set rstEMail = Nothing
  42.  
  43. DoCmd.OpenReport "AgencyUpdateForm", acViewPreview, , "[Agency Number]= forms![LeadershipEmailListALL].[Agency Number]"
  44. DoCmd.SendObject acSendReport, "AgencyUpdateForm", acFormatPDF, stTo, stCC, stBcc, stSubjectSummary, stMessageSummary
  45.  
  46.  
  47. Exit_Command43_Click:
  48.     Exit Sub
  49.  
  50. Err_Command43_Click:
  51.     MsgBox "No contacts selected. Please try again."
  52.     Resume Exit_Command43_Click
  53. End Sub
  54.  
Between the two, I feel like I'm close. But I've been working at this too long, and I'm beginning to think I'm missing something obvious.

Any input would be much appreciated. Thanks!
Jul 14 '10 #1

✓ answered by Delerna

Just been reading through the code you say you can't get the report to attach to more than one record.

Your code
1) creates a recordset from qryLeadershipEmailListALL
I presume that that query is selecting all the email addresses that the user has selected on the form
2) for each record retrieved
it runs the report based on the value in forms![LeadershipEmailListALL].[Agency Number]
and emails it



It seems to me that
forms![LeadershipEmailListALL].[Agency Number] is not the right place to get that value from because it will allways retrieve the same value. The value in the record that happens to have focus when the email is sent. This is exactly what you say is happening....I think.


Can you not retrieve the value you need in the qryLeadershipEmailListALL along with the email address. Then you can do this

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "AgencyUpdateForm", acViewPreview, , "[Agency Number]=" & rec("Agency Number")
  2.  

Also you may need to close that report after it has been sent

I hope I am understanding your post correctly

16 3757
Delerna
1,134 Expert 1GB
Don't know the cause of your issues and I couldn't problem solve the posted code, it's too complicated a problem to debug code in a forum post. That's why access has a debugging tool, so you can see what the code is doing as it is being executed and you can tell why it's not doing what you thought it would do when you wrote it.

Not my strong point either but I dabble with this sort of stuff every now and then.
I don't know if what I do is optimal, best practise or not. I do try to do things optimally but my underlying philosophy is if it works and it's performance is acceptable then that will do, move on to the next project.

Anyway here is something I have done in the past that maybe you can find usefull here ??

Maybe if you output the report as a file and then attach the file to the email
Expand|Select|Wrap|Line Numbers
  1.     DoCmd.OutputTo acOutputReport, "rptCustomersClearanceLetter", acFormatSNP, "BOL\CCL.SNP"
  2.     DoCmd.OutputTo acOutputReport, "rptCustomersClearanceLetter", acFormatRTF, "BOL\CCL.rtf"
  3.  
I generally get best results with the snp file
Jul 15 '10 #2
bre1603
39
@Delerna
I'm trying to get around saving each report, as there are around 400 contacts in the database.

Plus I don't think my users are going to want to go find 40 reports and attach them to the 40 emails they want to send out monthly (they want to send the 400 emails in waves of 40).

I'm fairly certian of the user's reluctance to this, because I already built in a way to send an email to one contact at a time with the report attached automatically (using the SendObject method), and that's not "easy enough" for them.

Hence my quest to do it by bulk. Ah, I love users...
Jul 16 '10 #3
Delerna
1,134 Expert 1GB
I was actually suggesting that you programmatically attach the files to the emails similar to what you are trying to do with the report or perhaps via CDO. I was not suggesting for your users to go find them and attach them. If I was a user I would baulk at that too!

However if you say it won't work then it wont work...... Anyone else?
Jul 16 '10 #4
bre1603
39
It sort of works... but only in combination with the SendObject method. And it attaches the same page of the report to every email. I want it to attach different pages - each agency should get it's own contact info attached.

There also seems to be some limit to the number of emails I can generate at once.

I'll keep trying.
Jul 16 '10 #5
Delerna
1,134 Expert 1GB
Just been reading through the code you say you can't get the report to attach to more than one record.

Your code
1) creates a recordset from qryLeadershipEmailListALL
I presume that that query is selecting all the email addresses that the user has selected on the form
2) for each record retrieved
it runs the report based on the value in forms![LeadershipEmailListALL].[Agency Number]
and emails it



It seems to me that
forms![LeadershipEmailListALL].[Agency Number] is not the right place to get that value from because it will allways retrieve the same value. The value in the record that happens to have focus when the email is sent. This is exactly what you say is happening....I think.


Can you not retrieve the value you need in the qryLeadershipEmailListALL along with the email address. Then you can do this

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "AgencyUpdateForm", acViewPreview, , "[Agency Number]=" & rec("Agency Number")
  2.  

Also you may need to close that report after it has been sent

I hope I am understanding your post correctly
Jul 19 '10 #6
Delerna
1,134 Expert 1GB
Also, Why do you say there seems to be a limit to the quantity of emails.

I think you might mean because it didn't retrieve that last contact selected.

If that is the case then are you aware that changes to a record on an access form are not actually updated to the table until after you move away from that record.

This means that if a user ticks 1 contact and then immediately clicks the email button, then that tick will not have been updated to the table yet and no-one would get emailed.

If you ticked 2 contacts and then clicked the email button, only 1 email would occur...the first one ticked.

If 3 were ticked then only the first 2 would get emailed.

I don't know how you have designed it so I may be way off here but it may be an answer
Jul 19 '10 #7
bre1603
39
I tried retreiving the value for [Agency Number] from the qryLeadershipEmailListALL, but I received the error**:
"Property not found. Runtime error 3270" when I reference rec("Agency Number"). My code looks exactly as yours above does, but I also tried it with brackets around [Agency Number], to no avail.

**Error is actually 3464, Data type mismatch in criteria expression.


Also, I think you're right about the problem I'm having getting emails to open for all the selected records. It's also because I wasn't accounting for null values in the email address field.
Jul 19 '10 #8
Delerna
1,134 Expert 1GB
you don't want to put the square braces in
rec("Agency Number") it is correct as it is.

The error suggests to me that the field [Agency Number] either does not exist in the query or it is spelled differently.

Check the query design and ensure that it is being selected in the query "qryLeadershipEmailListALL SelectQuery"
Jul 20 '10 #9
bre1603
39
I checked the query and the spelling, everything seems to match up. I'll include my current code - maybe you'll see something I can't.

Expand|Select|Wrap|Line Numbers
  1. Dim ol As Object
  2. Dim msg As Object
  3. Dim rec As DAO.Recordset
  4. Dim intCount As Integer
  5. Dim intLoop As Integer
  6.  
  7.  
  8. Set ol = CreateObject("Outlook.Application")
  9.  
  10. Set rec = CurrentDb.OpenRecordset("qryLeadershipEmailListALL SelectQuery")
  11. If Not (rec.BOF And rec.EOF) Then
  12. rec.MoveLast
  13. rec.MoveFirst
  14. intCount = rec.RecordCount
  15. For intLoop = 1 To intCount
  16. Set msg = ol.CreateItem(olMailItem)
  17. msg.Subject = "Contact Information for: " & rec("Agency Name")
  18. msg.To = rec("Leadership Email")
  19. msg.CC = ""
  20. msg.Body = "This is a periodic request for your up-to-date information with which to contact your agency."
  21. msg.Body = msg.Body & Chr(13) & Chr(13) & "If you have any questions, please reply or call me at 555-555-5555."
  22.  
  23. DoCmd.OpenReport "AgencyUpdateForm", acViewPreview, , "[Agency Number]=" & rec("Agency Number")
  24. DoCmd.OutputTo acOutputReport, "AgencyUpdateForm", acFormatPDF, "F:\" & [Agency Name] & ".pdf"
  25. DoCmd.SendObject acSendReport, "AgencyUpdateForm", acFormatPDF, msg.To, , , msg.Subject, msg.Body
  26.  
  27. msg.Display
  28.  
  29. Set msg = Nothing
  30. rec.MoveNext
  31. Next intLoop
  32. End If
  33.  
  34. Set rec = Nothing
  35. Set ol = Nothing
  36.  
  37. End Sub
The code errors at line 23.

Please tell me it's just a misplaced comma or something...
Jul 20 '10 #10
Delerna
1,134 Expert 1GB
looking at that now I think I see the problem.
I copied your code for rec("Leadership Email") and changed the field name. I didn't notice that this is not right.

it should be
rec.fields("Leadership Email")
and
rec.fields("Agency Number")

I am puzzled now as to why your original
rec("Leadership Email")
did not generate the same error
Jul 21 '10 #11
bre1603
39
I tried adding .fields to both lines of code, but it had no effect. I still get that same error, "Type mismatch."

It occurred to me though...does it matter that [Agency Number] is a text field? The "numbers" range from "011" to "310 A01" to "956" - could this be the causing the error? the field is a text field throughout (in table, query, form, and report), but perhaps in this code I'm not accounting for that? Just a thought...
Jul 21 '10 #12
bre1603
39
I've got the code successfully opening an email window for the selected contacts. And it's attaching what I told it to (when I alter/take out the rec.Fields() part...) But it's not limiting the report it's outputting - so the entire report gets attached to each email.

With the rec.fields() included, I get the Data Type Mismatch error. Without it, I get the whole report, since that's where I'm trying to apply a filter on the report as it's opened. I'm going to attach my updated code since I was tweaking it today. I really don’t know where to go from here.

Expand|Select|Wrap|Line Numbers
  1. Dim ol As Object
  2. Dim msg As Object
  3. Dim rec As DAO.Recordset
  4. Dim intCount As Integer
  5. Dim intLoop As Integer
  6. Dim dbs As DAO.Database
  7. Dim strAgNo As String
  8.  
  9. Set ol = CreateObject("Outlook.Application")
  10. Set dbs = CurrentDb
  11. Set rec = dbs.OpenRecordset("qryLeadershipEmailListALL SelectQuery")
  12.  
  13. If Not (rec.BOF And rec.EOF) Then
  14. rec.MoveLast
  15. rec.MoveFirst
  16. intCount = rec.RecordCount
  17. For intLoop = 1 To intCount
  18. Set msg = ol.CreateItem(olMailItem)
  19. msg.Subject = "Contact Information for: " & rec("Agency Name")
  20. msg.To = rec.Fields("Leadership Email")
  21. msg.CC = ""
  22. msg.Body = "This is a periodic request for your up-to-date information with which to contact your agency."
  23. msg.Body = msg.Body & Chr(13) & Chr(13) & "If you have any questions, please reply or call me at 555-555-5555."
  24. strAgNo = rec.Fields("Agency Number")
  25.  
  26. DoCmd.OpenReport "AgencyUpdateForm", acViewPreview, "[Agency Number] =" & rec.Fields("Agency Number")
  27. DoCmd.OutputTo acOutputReport, , acFormatPDF, "F:\" & [Agency Name] & ".pdf"
  28. 'DoCmd.SendObject acSendReport, "AgencyUpdateForm", acFormatPDF, msg.To, , , msg.Subject, msg.Body
  29. DoCmd.Close acReport, "AgencyUpdateForm", acSaveNo
  30.  
  31. msg.Attachments.Add "C:\Documents and Settings\bleb107\Desktop\Initial Email Msg.doc"
  32. msg.Attachments.Add "F:\" & [Agency Name] & ".pdf"
  33. msg.Display
  34.  
  35. Set msg = Nothing
  36. rec.MoveNext
  37. Next intLoop
  38. End If
  39.  
  40. Set rec = Nothing
  41. Set ol = Nothing
Thanks for your help.
Jul 21 '10 #13
Delerna
1,134 Expert 1GB
I not sure at this stage how to assist you. As I said previously it's difficult to problem solve code on a forum post. I cannot see the database in it's entirety, or set breakpoints so as to monitor what is happening as it runs. The database probably contains emails that you wouldn't and shouldn't want to make available to anyone by attaching the database here.



The problem is here
Expand|Select|Wrap|Line Numbers
  1.  "[Agency Number] =" & rec.Fields("Agency Number") 
  2.  
When you originally had it as

Expand|Select|Wrap|Line Numbers
  1.  "[Agency Number] = forms![LeadershipEmailListALL].[Agency Number]") 
  2.  

Did the report obtain the correct data for that agency number?
If it did then you should be able to get this working!





The error is saying that the data type for this [Agency Number]
does not match the data type for this rec.Fields("Agency Number")
ie .. [Agency Number] is expecting an int
and rec.Fields("Agency Number") is being interpreted as string .. for example.

What is the data type of [Agency Number]?

Some things to try if [Agency Number] is string
Expand|Select|Wrap|Line Numbers
  1.  "[Agency Number] ='" & rec.Fields("Agency Number") & "'"
  2.  
  3. or
  4.  
  5.  "[Agency Number] ='" & rec.Fields("Agency Number").value & "'"
  6.  

Some things to try if [Agency Number] is int
Expand|Select|Wrap|Line Numbers
  1.  "[Agency Number] =" & cint(rec.Fields("Agency Number")) 
  2.  
  3. or
  4.  
  5.  "[Agency Number] =" & cint(rec.Fields("Agency Number").value) 
  6.  
Jul 22 '10 #14
bre1603
39
So I was exploring my thoughts on the problem stemming from the fact that [Agency Number] is a text field... And I came up with what you did -

Expand|Select|Wrap|Line Numbers
  1. "[Agency Number] ='" & rec.Fields("Agency Number") & "'" 
  2.  
And that worked - I got three emails to open with the correct attachement for each! However, all three attachments had the same name (which should be the Agency Name, therefore different for each attachment).

Is there a way to solve this do you think? At worst, I'll just make the attachment name something generic, but I'd like it to be the Agency Name if possible. Here's the code where I name it:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputReport, , acFormatPDF, "F:\" & [Agency Name] & ".pdf" 
  2.  
Thanks for all your help!
Jul 22 '10 #15
Delerna
1,134 Expert 1GB
Is agency name in the query?

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputReport, , acFormatPDF, "F:\" & rec.Fields("Agency Name") & ".pdf" 
  2.  
Jul 22 '10 #16
bre1603
39
That seemed obvious - reference the rec.fields("Agency Name")... But it still snuck past me. Thanks!

I've got the whole thing working like a dream now. Thank you so much for all your help. You were a life-saver!
Jul 22 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sami | last post by:
I can create queries and reports based on info from one table. How do I create one using information from multiple tables. What do I need to make sure the information from one table will be...
7
by: Mega1 | last post by:
is this possable to send more than 1 report in one email
4
by: acni | last post by:
hey just wondering if anyone could offer a bit of advice to me.i have a database with a list of contacts on it with a checkbox beside each one.i was wondering if it was possible to place a...
1
by: nabil m | last post by:
hi i have 5 checkboxes i would like to when the user click on 1 or multiple checkbox i would like to email 1 or multiple files attachments to them ex: mailMsg.Attachments.Add(myAttachment+i); but...
9
by: Jon Davis | last post by:
I am working with an application that is compiled as a COM EXE (written in Delphi 7). I don't know if it's the nature of COM EXE or if it's implemented in the COM EXE, but when I create a new...
2
by: Andrew Butchart | last post by:
We have an intranet application that hosts internal pdf documents about our products. I've recently discovered that the users have a very tedious time whenever they have to send multiple documents...
7
by: Ceebaby via AccessMonster.com | last post by:
Hi All Here's hoping someone can help me with this. I have a report based on a query where the criteria for 4 of the fields is set from an unbound form. I want the user to be able to select any...
7
by: KiwiGenie | last post by:
I have a form which lists selected records, there could be anything from 1 record to all records selected. I want to output a report for each record as HTML. I want each file to take its name from a...
1
by: gwise | last post by:
I have an access database that is for a manufacturer. There are multiple process that each one of their jobs has to go through in order to be completed. They want to create a report that shows them...
4
by: Trish | last post by:
Hello All and thanks in advance for your help. The code below works just fine in the Print event of the report Detail section. Is it possible to condense it using a loop or something? Can you...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.