I've made the following program that takes a list of email addresses from a table in MS Access and sends each a test email. Here is the code:
'Get data back from field birth date
Set fld = rcdSet.Fields("Email Address")
'Subject
olMailItem.Subject = "Mailing List Test"
'Loop through the records
For i = 0 To intNumRecords
'Recipient/s
olMailItem.To = fld.Value
'Body of email
olMailItem.Body = strBodyText
'Automatically send the email
olMailItem.Send
'Reset email item otherwise it won't work
Set olMailItem = olFolder.Items.Add("IPM.Note")
'Move to the next record
rcdSet.MoveNext
Next
And yes I opened a record set but haven't included that code above. So here's my questions:
Is my approach above correct? I had to reset the
olMailItem
in the loop otherwise it would return aType Error
. Is there a better approach to sending multiple emails?I put in an invalid email to see what happens - it causes another
Type Error
. Is there anyway to detect the bounce back (the email outlook sends to you informing you that it was a bad address) email and send a message to theImmediate Window
(For dev purposes at this point)
Thanks
Note - Have added declaration of mail items
'Create Outlook object
Dim olApp As Outlook.Application
'Namespace
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
'Create a reference to the email item you will use to send the email
Dim olMailItem As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
Set olMailItem = olFolder.Items.Add("IPM.Note")
If I don't re-set the olMailItem in the for loop an error occurs on the .To
part of the program - the aforementioned Type Error