from a db, send one email to multiple people

armesca

Registered User.
Local time
Yesterday, 23:43
Joined
Apr 1, 2011
Messages
45
I have code in my access db that selects certain email addresses from a table, opens outlook and sends them an email. I can't figure out how to add multiple email adresses to one email. any help is appreciated:

' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application

' Set up the database and query connections

Set db = CurrentDb()
mysql3 = "SELECT [User_Email] From [User_Data] Where [Access Level] =" & 4
Set rsemail = db.OpenRecordset(mysql3)

'
' this is where we loop through our list of addresses,
' adding them to e-mails and sending them.

'Do Until rsemail.EOF

' This creates the e-mail

Set MyMail = MyOutlook.CreateItem(olMailItem)

Do Until rsemail.EOF
' This addresses it

MyMail.To = rsemail(0) 'maillist("email")

'This gives it a subject
MyMail.Subject = Subjectline$

'This gives it the body
MyMail.Body = MyBodyText
MyMail.Display

'And on to the next one...
rsemail.MoveNext

Loop

'Cleanup after ourselves
 
It appears that you have inherited or found some code to send an email. By default it is only sending to one addressee.
To put multiple recipients into the mailTo you will need to loop through the email addresses. You need to create a string such as

MyRecipients = "recipient1; recipient2; recipientX"

Here is some adjusted code to do the multi recipients. You'll still have to deal with Subject and Body.

Code:
Sub awfSendEmailMultiTo()

[COLOR="Green"]Dim MyRecipients As String[/COLOR]  ' a string variable for multi recipients

' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application

' Set up the database and query connections

Set db = CurrentDb()
mysql3 = "SELECT [User_Email] From [User_Data] Where [Access Level] =" & 4

Set rsemail = db.OpenRecordset(mysql3)

'
' this is where we loop through our list of addresses,
' adding them to e-mails and sending them.

'Do Until rsemail.EOF

' This creates the e-mail

Set MyMail = MyOutlook.CreateItem(olMailItem)
[COLOR="Purple"]' Code to process all recipients and build a list[/COLOR]
[COLOR="Green"]Do Until rsemail.EOF
' This addresses it
MyRecipients = MyRecipients & rsemail!User_Email & ";"
rsemail.MoveNext
Loop
'Remove final ";" from MyRecipients
MyRecipients = Left(MyRecipients, Len(MyRecipients) - 1)
Debug.Print MyRecipients
' Finished creating MyRecipients list for Outlook[/COLOR]

'MyMail.To = rsemail(0) 'maillist("email")

[COLOR="Green"]MyMail.To = MyRecipients[/COLOR]
'This gives it a subject
MyMail.Subject = Subjectline$

'This gives it the body
MyMail.body = MyBodyText
MyMail.Display

'And on to the next one...


'Cleanup after ourselves
End Sub

Hope this helps.
 

Users who are viewing this thread

Back
Top Bottom