HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Sending test emails to a list of email addresses

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
emailsendingaddressestestemailslist

Problem

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.

```
'Send email to mailing list
Function SendNotificationEmail()

'Handle any errrors
On Error GoTo ErrHandler:

'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")

'Create the body of the message
Dim strBodyText As String
strBodyText = "Test Email - DELETE ME :D"

'Open record set on
Dim dbC As DAO.Database
Dim rcdSet As DAO.Recordset
Dim fld As DAO.field

'Used to update the table
Dim strSQL As String
strSQL = "SELECT DISTINCT [Email Address] FROM tblMailingList"

'Connect to current database
Set dbC = CurrentDb
Set rcdSet = dbC.OpenRecordset(strSQL)

'Count the number of records
Dim intNumRecords As Integer
'Move to the last record in order to count all the records
rcdSet.MoveLast

intNumRecords = rcdSet.RecordCount - 1
'Move back
rcdSet.MoveFirst

'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")

Solution

Set olMailItem = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing


is unnecessary. VBA is garbage collected, there's no need for this, or any of the other = Nothing's you have. Here's an article from one of the MS developers about this practice. It refers to VBScript, but VBA's garbage collection algorithm is similar, and probably substantially better than VBScript's.

Around

'Open record set on
        Dim dbC As DAO.Database


You start a new level of indentation. Why? I thought when I first read this that it was a part of the loop below.

Finally, you might want to think more about your error handling (although VB's error handling isn't very fun to work with). For example, if you get an exception on olMailItem.Send, you might not care -- you want the script to keep going and try to send the next email, in case that was a fluke. However, if something goes wrong on Set rcdSet = dbC.OpenRecordset(strSQL), you probably want the script to quit entirely, since nothing afterwards is going to be useful if you don' have anything to work on.

Code Snippets

Set olMailItem = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
'Open record set on
        Dim dbC As DAO.Database

Context

StackExchange Code Review Q#30130, answer score: 3

Revisions (0)

No revisions yet.