Auto Create Emails in Outlook based on Data in Access (1 Viewer)

ConfusedBex

New member
Local time
Today, 11:44
Joined
Apr 20, 2015
Messages
5
Hi, I'm a very new to VBA so I'm hoping what I'm looking for isn't too complex, or maybe someone might already have something written that might be able to help me out with the below.

I have a table called Contacts in Access:
ID Email
001 email1
002 email2
003 email3
004 email4
008 email5
010 email6

And I have a table called Data in Access:

ID Date Person Title Yes/No Action
001 20/4 Mr A Consultant

001 20/4 Mr B Supervisor

001 20/4 Mr C Consultant

004 20/4 Mr D Consultant

010 20/4 Mr E Consultant

010 20/4 Mr F Supervisor


What I’m looking for is coding that would start with the first ID number in CONTACTS and see if there is the corresponding ID in DATA. If there is, then I want it to create an Outlook email and have:
1.To field populated with the email address in the CONTACTS table
2.From and CC fields to be my email address
3.The subject line needs to read [ID] “Request” and then today’s date backward (e.g. 20150420).
4.The body I need to put some generic wording and then a table with the records from the DATA table (first 5 fields plus the headings).
5. The format of the table etc isn't important as long as it is a table format
If it’s created the email I want it to go back to the DATA table and put “Email Created” in the Action column for those IDs.
I don’t want the email to automatically send; I only want it to create a draft (as this data needs to be manually confirmed before sending).
If for example it gets to checking in DATA table for ID 002 (and as seen there’s none there), I want it to skip creating the email and move on to the next ID.
In the end I would expect to see 3 draft emails open in Outlook ready to be sent, looking something like the below (just 001 below)?

From... my email
To... Email1
CC... my email
Subject: 001 Request 20150420

Hi,
Please action the below:

ID Date Person Title Yes/No
001 20/4 Mr A Consultant
001 20/4 Mr B Supervisor
001 20/4 Mr C Consultant

Regards,
Me.

And the DATA table look like this (if only 001 email was created):

ID Date Person Title Yes/No Action
001 20/4 Mr A Consultant Email created
001 20/4 Mr B Supervisor Email created
001 20/4 Mr C Consultant Email created
004 20/4 Mr D Consultant
010 20/4 Mr E Consultant
010 20/4 Mr F Supervisor

I will need to add more data to the CONTACTS table over time, also the DATA table will be different each day so I need the coding to accomodate for both of these possibilities.

Let me know if this is quite an easy thing, or if it is more difficult :)
Thanking you in advance!!!
 

Minty

AWF VIP
Local time
Today, 02:44
Joined
Jul 26, 2013
Messages
10,371
There are many threads on here regarding automatic creation of emails.

In your case you need to initially create the queries that will pull in the dataset you need for the email.

Once you have the data you can then experiment with creating a single email with your data in it, in the format you need.

Then we can assist you with creating a loop process to create the multiple emails you need.
 

Trevor G

Registered User.
Local time
Today, 02:44
Joined
Oct 1, 2009
Messages
2,341
Welcome to the Forum,

What have you tried so far?
 

ConfusedBex

New member
Local time
Today, 11:44
Joined
Apr 20, 2015
Messages
5
This is what I have so far... not I've used numerous other threads to piece together this. I've only got it to create a new email for every ID in the CONTACT table with generic wording... (also not sure if the code will come up right)

Code:
 [FONT=Calibri]Public Function SendEmailAuto()

[/FONT][FONT=Calibri]Dim db As DAO.Database
[/FONT][FONT=Calibri]Dim MailList As DAO.Recordset
[/FONT][FONT=Calibri]Dim MyOutlook As Outlook.Application
[/FONT][FONT=Calibri]Dim MyMail As Outlook.MailItem
[/FONT][FONT=Calibri]Dim Subjectline As String
[/FONT][FONT=Calibri]Dim BodyFile As String
[/FONT][FONT=Calibri]Dim fso As FileSystemObject[/FONT][FONT=Calibri] [/FONT]
 [FONT=Calibri]Set fso = New FileSystemObject[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]' First, the subject in the email, ***in addition I need it to show the first ID number in CONTACTS  that also shows in DATA before the stuff in the below[/FONT]
 [FONT=Calibri]Subjectline$ = "Request  " & Format(Date, "yyyyMMdd")[/FONT]
 [FONT=Calibri]'Second, the body in the email, ***in addition I need a table in here from the DATA table with the items that have the same ID number showing in the subject line[/FONT]
 [FONT=Calibri]BodyFile$ = "Hi Team," & vbNewLine & vbNewLine & "Please validate the below: " & vbNewLine & vbNewLine & vbNewLine & "Regards," & vbNewLine & "Me"[/FONT]
 [FONT=Calibri]' Open Outlook[/FONT]
 [FONT=Calibri]Set MyOutlook = New Outlook.Application[/FONT]
 [FONT=Calibri]' Set up the database and query connections[/FONT]
 [FONT=Calibri]Set db = CurrentDb()
[/FONT][FONT=Calibri]Set MailList = db.OpenRecordset("CONTACTS")[/FONT]
 [FONT=Calibri]' I think this is where it should check if there is ID in DATA table corresponding with the CONTACTS table?[/FONT]
 [FONT=Calibri]' then if  there is ID in DATA table, continue with creating the email as below:[/FONT]
 [FONT=Calibri]    ' loop through and add them to e-mails.[/FONT]
 [FONT=Calibri]    Do Until MailList.EOF[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    ' This creates the e-mail[/FONT]
 [FONT=Calibri]    Set MyMail = MyOutlook.CreateItem(olMailItem)[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    'This send on behalf of[/FONT]
 [FONT=Calibri]    MyMail.SentOnBehalfOfName = "Group email I want to send on behalf of "[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    ' This addresses it[/FONT]
 [FONT=Calibri]    MyMail.To = MailList("Contact")[/FONT]
 [FONT=Calibri]    ' in addition if there isn’t an email address in the, can I get it to create an email and leave the TO field blank?[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    ' This CCs me[/FONT]
 [FONT=Calibri]    MyMail.CC = " My Email as an email address "[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    'This gives it the subject from above[/FONT]
 [FONT=Calibri]    MyMail.Subject = Subjectline$[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    'This gives it the body from above[/FONT]
 [FONT=Calibri]    MyMail.Body = BodyFile$[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    'This shows it in draft ready to hit send button![/FONT]
 [FONT=Calibri]     MyMail.Display[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]    'And on to the next one in the CONTACTS list...[/FONT]
 [FONT=Calibri]    MailList.MoveNext[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]' I assume then I would use ELSE if there isn't the ID in the table don’t create the email and instead loop back up and do the next one?[/FONT]
 [FONT=Calibri] [/FONT][FONT=Calibri]‘ Currently this loop just goes back to the next CONTACT record and creates the next email[/FONT]
 [FONT=Calibri]Loop[/FONT]
 [FONT=Calibri] [/FONT]
 [FONT=Calibri]' Cleanup after ourselves[/FONT]
 [FONT=Calibri]Set MyMail = Nothing[/FONT]
 [FONT=Calibri]Set MyOutlook = Nothing[/FONT]
 [FONT=Calibri]MailList.Close[/FONT]
 [FONT=Calibri]Set MailList = Nothing[/FONT]
 [FONT=Calibri]db.Close[/FONT]
 [FONT=Calibri]Set db = Nothing[/FONT]
 [FONT=Calibri]End Function[/FONT]
 

Trevor G

Registered User.
Local time
Today, 02:44
Joined
Oct 1, 2009
Messages
2,341
Rather than using the table as Minty has suggested create a query and use the query as the data source.
 

ConfusedBex

New member
Local time
Today, 11:44
Joined
Apr 20, 2015
Messages
5
Ok, so I've created the query so now the query outcome looks like:
ID DATE Person Yes/No Email
001 20/4 Mr A email1
001 20/4 Mr B email2
001 20/4 Mr C email3
004 20/4 Mr D email4

How do I get all the records with e.g. ID 001 in the email body?
I assume once the looping goes in it will loop through each ID number instead of each record in the query too.

Code:
Set db = currentdb ()
Set maillist = db.openrecordset("query1")

' the subject line.... the below puts in the first ID against all emails
subjectline$ = maillist ("ID") & " request " & format (date, "yyyyMMdd")

'Body of email,)
Bodyfile$ = "hi team," & vbnewline & "please validate the below:" & vbnewline & "Regards, Me"
 

Wayne

Crazy Canuck
Local time
Yesterday, 21:44
Joined
Nov 4, 2012
Messages
176
I'm trying to do the same thing in an e-mail, and my Outlook calendar. I have a contracting business, and on my Orders form (which details a single client), I have a sub-form that lists all the items to be done at that client's home (a different record for each work item). When I send an e-mail, or post the appointment to the Outlook calendar, I am trying to list all those records in the body of the e-mail, and/or in the Notes section of the appointment. I created the query to call the records, but have not figured out a way to call it in the code. I have searched the forums for endless hours looking for the solution, but this is the closest I got. Can anyone point me in the right direction?

Any help would be appreciated. Thank you.
 

ConfusedBex

New member
Local time
Today, 11:44
Joined
Apr 20, 2015
Messages
5
OK so I've changed what I have as I found some other coding where the query data is in the email body in table format (which looks pretty).

At the moment: One email is created per record in my query, with the same email contact (in the To field) and same ID (in the Subject line). The email includes my signature on the bottom as well.

What I really want: All records with ID 001, in one email together, then one email with all records with ID 002 in another... etc...
I think I might have the looping in the wrong spot but not sure where it can go.
I've tried doing a If function (basically if next record ID = the same ID as the record above loop back and add it to the email, if not move on and start a new email) but that doesn't work at all...

Any help would be appreciated, thanking you :)

Code:
 [FONT=Calibri][SIZE=3]Public Function ViewAutoEmail()[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim olApp As Outlook.Application[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim objMail As Outlook.MailItem[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim ObjAttach As Outlook.Attachments[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim strTblQryName As String[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim strMsg As String[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim sqlString As String[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim i As Integer[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim rowColor As String[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim db As DAO.Database[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim rs As DAO.Recordset[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Dim MailList As DAO.Recordset[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set db = CurrentDb[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set rs = db.OpenRecordset("[/SIZE][COLOR=#0070c0][SIZE=3]Query1[/SIZE][/COLOR][SIZE=3]")[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set MailList = db.OpenRecordset("[/SIZE][COLOR=#0070c0][SIZE=3]Query1[/SIZE][/COLOR][SIZE=3]")[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]sqlString = "SELECT * FROM " & strTblQryName & ""[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]'GET EMAIL SIGNATURE[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Signature = Environ("appdata") & "\Microsoft\Signatures\" & "[/SIZE][COLOR=#0070c0][SIZE=3]Template.htm[/SIZE][/COLOR][SIZE=3]"[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]If Dir(Signature, vbDirectory) <> vbNullString Then[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]    [/SIZE][SIZE=3]Signature = Signature & Dir$(Signature & "*.htm")[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Else:[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]    [/SIZE][SIZE=3]Signature = ""[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]End If[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]    [/SIZE][SIZE=3]Signature = CreateObject("Scripting.FileSystemObject").GetFile(Signature).OpenAsTextStream(1, -2).ReadAll[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Do While Not rs.EOF[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]strMsg = "<table border='1' cellpadding='3' cellspacing='3' style='border-collapse: collapse' bordercolor='#111111' width='800'>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"<tr>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"<td bgcolor='#7EA7CC'> <b>[/SIZE][COLOR=#0070c0][SIZE=3]Date[/SIZE][/COLOR][SIZE=3]</b></td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"<td bgcolor='#7EA7CC'> <b>[/SIZE][COLOR=#0070c0][SIZE=3]ID[/SIZE][/COLOR][SIZE=3]</b></td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"<td bgcolor='#7EA7CC'> <b>[/SIZE][COLOR=#0070c0][SIZE=3]Person[/SIZE][/COLOR][SIZE=3]</b></td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"<td bgcolor='#7EA7CC'> <b>[/SIZE][COLOR=#0070c0][SIZE=3]Yes/No?[/SIZE][/COLOR][SIZE=3]</b></td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"</tr>"[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]i = 0[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]If (i Mod 2 = 0) Then[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rowColor = "<td bgcolor='#FFFFFF'> "[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Else[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rowColor = "<td bgcolor='#E1DFDF'> "[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]End If[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]strMsg = strMsg & "<tr>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rowColor & rs.Fields("[/SIZE][COLOR=#0070c0][SIZE=3]Date[/SIZE][/COLOR][SIZE=3]") & "</td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rowColor & rs.Fields("[/SIZE][COLOR=#0070c0][SIZE=3]ID[/SIZE][/COLOR][SIZE=3]") & "</td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rowColor & rs.Fields("[/SIZE][COLOR=#0070c0][SIZE=3]Person[/SIZE][/COLOR][SIZE=3]") & "</td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rowColor & rs.Fields("[/SIZE][COLOR=#0070c0][SIZE=3]Yes/No?[/SIZE][/COLOR][SIZE=3]") & "</td>" & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"</tr>"[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]strMsg = strMsg & "</table>"[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set olApp = Outlook.Application[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set objMail = olApp.CreateItem(olMailItem)[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]With objMail[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3].BodyFormat = olFormatHTML[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3].HTMLBody = "[/SIZE][COLOR=#0070c0][SIZE=3]Hi Team,[/SIZE][/COLOR][SIZE=3]" & "<br>" & "<br>" _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]& "[/SIZE][COLOR=#0070c0][SIZE=3]Please validate the below:[/SIZE][/COLOR][SIZE=3] " & _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]"<br>" & "<br>" _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]& strMsg & "<br>" & "<br>" _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]& "[/SIZE][COLOR=#0070c0][SIZE=3]Regards[/SIZE][/COLOR][SIZE=3]," & "<br>" & "<br>" _[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]& Signature[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3].SentOnBehalfOfName = "[/SIZE][COLOR=#0070c0][SIZE=3]Goup Mailbox[/SIZE][/COLOR][SIZE=3]"[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3].To = MailList("[/SIZE][COLOR=#0070c0][SIZE=3]Contact[/SIZE][/COLOR][SIZE=3]")[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3].CC = "[/SIZE][COLOR=#0070c0][SIZE=3] Goup Mailbox[/SIZE][/COLOR][SIZE=3] "[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3].Subject = MailList("[/SIZE][COLOR=#0070c0][SIZE=3]ID[/SIZE][/COLOR][SIZE=3]") & " [/SIZE][COLOR=#0070c0][SIZE=3]POV[/SIZE][/COLOR][SIZE=3] [/SIZE][COLOR=#0070c0][SIZE=3]Request[/SIZE][/COLOR][SIZE=3] " & Format(Date, "yyyyMMdd")[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3].Display[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]End With[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]rs.MoveNext[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]i = i + 1[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Loop[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set olApp = Nothing[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]Set objMail = Nothing[/SIZE][/FONT]
 [FONT=Calibri][SIZE=3] [/SIZE][/FONT]
 [FONT=Calibri][SIZE=3]End Function[/SIZE][/FONT]
 

ConfusedBex

New member
Local time
Today, 11:44
Joined
Apr 20, 2015
Messages
5
I'm still stuck on getting this to work 100%.
With the coding above, it creates an email per record in my query, I really want all records with ID 1 to be put together in one email, all records with ID 2 in another email etc...

Any help would be REALLY great!!
 

Users who are viewing this thread

Top Bottom