Use the Recipients collection to add recipients to emails

DC asks for more best practices. One thing does come to mind.

I see a lot of Outlook VBA code that uses MailItem.To, MailItem.CC and MailItem.BCC to set or get the list of recipients for a given item.

For example, Ron de Bruin does this in a series of MSDN articles and on his website:

Another example:

Sending Email From IIS with ASP and CDONTS

And yes, even Microsoft does it:

Sending E-Mail Without Installing the SMTP Service

For your review I humbly submit the following as a best practice: Use the Recipients Collection to manipulate Item Recipients.

Before you say anything, I understand the following:

  • VBA allows you to use To/CC/BCC properties to set recipients. If you were supposed to use the Recipients Collection instead, these properties would be read-only.
  • It is simpler to write ".To =" than the way I suggest, which is to set a reference to MailItem.Recipients, create the recipient and assign it to the "To" field.

Instead of (assume msg is a variable of type Outlook.MailItem):

msg.To = ""

you would write

Dim recips As Object ' Outlook.Recipients
Dim currentRecipient As Object ' Outlook.Recipient
Set recips = msg.Recipients
Set currentRecipient = recips.Add ""
currentRecipient.Type = olTo

In Working with the Outlook Recipients collection in VBA I explained my rationale for why you should use MailItem.Recipients. The article includes methods for adding, counting and removing recipients, which I will update shortly and should leave you with no excuse not to use the Recipients Collection. But I will repeat my reasons here anyway, in order to try and convince you.

  1. The Recipients Collection is the interface designed for interacting with Recipients
  2. The Recipients Collection is a more robust interface than .To, .CC, .BCC
  3. To, CC and BCC only contain the display names
  4. Microsoft wants you to use the Recipients Collection – I know this is a weak argument

If you didn't already know, I am a big fan of encapsulation. As a result, I am a big fan of using interfaces designed for interacting with an object. So I repeat: The Recipients Collection is the interface designed for interacting with Recipients.

If you use the To, CC or BCC properties in code you post publicly in order to set their values, I will correct you.

Let's put it this way: if you downloaded a XML file from the web, would you use Instr and Mid to find what you want, or would you use an interface (the MSXML DOM) that includes methods specifically designed to parse it?

If you have code that uses .To, .CC, .BCC, I encourage you to rewrite it using the methods I posted in Working with the Outlook Recipients collection in VBA. Let's collectively agree to use the methods designed for the purpose of manipulating Recipients. Your comments welcome.

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 1 Comment(s) on Use the Recipients collection to add recipients to emails:

  1. Thanks for writing this post in response to my comment! And please keep the posts coming, especially the best practice posts. I would say I'm still learning the basics of VBA, but I already am realizing how important it is to grasp best practices as early as possible.

    I plan on reading some of your prior posts as well, looks like you have quite a collection. I just wrote my first macro that uses Outlook and I'm excited about the possibilities. It amazes me more ppl in finance aren't trying to learn more about vba, seems it can give you a huge edge as far as working efficiently.

This article is closed to any future comments.
Peltier Tech Charting Utilities for Excel