Sending e-mail message from a form



I'm using the following code for a button on a form to
send an e-mail message:

PrivateSub Button_Click()

Dim olookApp As Outlook.Application
Dim olookMsg As Outlook.MailItem
Dim olookRecipient As Outlook.Recipient

' create the Outlook session.
Set olookApp = CreateObject("Outlook.Application")

' create the message.
Set olookMsg = olookApp.CreateItem(olMailItem)

With olookMsg
' add the To recipient(s) to the message.
Set olookRecipient = .Recipients.Add
("(e-mail address removed); (e-mail address removed)")
olookRecipient.Type = olTo

' set the Subject, Body, and Importance of the
.Subject = "This is an Automation test with
Microsoft Outlook"
.Body = "Last test" & vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance

' resolve each Recipient's name
For Each olookRecipient In .Recipients
If Not olookRecipient.Resolve Then
olookMsg.Display ' display any names that
can't be resolved
End If

End With
Set olookMsg = Nothing
Set olookApp = Nothing

End Sub

The code works, but the message is delivered only to the
last address in the list. The rest messages are returned
claiming on the address syntax error.
I tried to put comma instead of ";" between the addresses
in the list but in vain.
What is the problem I wonder?
Thanks for help!



Cheryl Fischer

Hello Mike,

You will need to add each recipient individually.

Set olookRecipient = .Recipients.Add "(e-mail address removed)"
olookRecipient.Type = olTo
Set olookRecipient = .Recipients.Add "(e-mail address removed)"
olookRecipient.Type = olTo

If you have quite a few recipients, you could create a recordset containing
all of the necessary email address and then loop through it to add
recipients, as the following code snippet shows.

Set objNewMail = oApp.CreateItem(olMailItem)
With objNewMail
Do While Not rs.EOF
If Len(Trim(rs!Email)) > 0 Then
Set objOutlookRecip = .Recipients.Add(rs!Email)
objOutlookRecip.Type = olTo
End If

' The rest of your code here

End With


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads