[ Automating Office with VBA ]

April 1999

Outlook's Email Message Items

Because Outlook's object model is structured a little differently than most Office applications, it's easy to quickly become frustrated when you start to program with it. Fortunately, nine times out of ten your exposure to the object hierarchy will be limited--you'll simply want to provide Outlook's emailing capability via Automation to another application. To accommodate this, we've decided to spotlight Outlook's MailItem object in this month's installment of the "AOV Reference Guide." (Note: Because Outlook 97 and Outlook 98 both use VBScript internally, all of our examples assume you're using VBA from an application other than Outlook. In addition, you should include a reference to the Outlook 98 Type Library.)

Object: MailItem

Simply put, a MailItem represents one mail message in your Outlook Inbox folder. To create a new email message, you create a new MailItem. To refer to an existing message, use the Items collection.

Items(index)

This collection contains all the Outlook objects from a specific folder. As with most members in a collection, you indicate a particular item by its index number or name. For purposes of a MailItem, the name is the text that appears in the Subject field. For instance, the following code:


Set olApp = New Outlook.Application

Set olFolder = olApp.GetNamespace("MAPI") _
	.GetDefaultFolder(olFolderInbox)

Set itmMailItem = olFolder _
	.Items("Per your request")
creates an object variable, itmMailItem, based on the email message with "Per your request" in the subject line. Note, however, that if you have two messages with the same subject, using the name as the index argument returns only the first message.

Recipients(index)

This collection represents the list of addressees indicated in the To field of an email message. Use the Add method to add a new recipient to a MailItem, as in


Set itmMail = OlApp.CreateItem(olMailItem)
itmMail.Recipients.Add "automating_w_vba@zdjournals.com"
How you refer to a Recipient by name depends on whether you've already entered it as a resource in Outlook or not. For example, say we had already entered Mike D. Jones as a resource in the Address Book. In the previous example, then, instead of the email address, we'd use


Set itmMail = OlApp.CreateItem(olMailItem)
itmMail.Recipients.Add "Mike D. Jones"

Methods: CreateItem(ItemType)

In general, you use this method to create a new Outlook item. For MailItems, use the olMailItem constant in the ItemType argument. This method is useful, because when used in conjunction with the Application object, there's no need to traverse Outlook's entire folder hierarchy just to create a new item. So, to open a new email message, you'd use


Set olApp = New Outlook.Application
Set itmMail = olApp.CreateItem(olMailItem)

Send, Reply, ReplyAll

These three commands work the same way as do the buttons on the toolbar. Use them to Send or Reply to a specific MailItem. For example, to mail the itmMail object we created earlier, enter the following line of code:


itmMail.Send
Outlook takes care of the rest. To reply to pre-addressed messages, replace Send from the previous example with Reply or ReplyAll, depending on which action you wish to carry out.

Properties: Subject

This property is just like the Outlook email field. It sets the text that appears in the Subject line header. The line of code


itmMail = "Per your request"
is the equivalent of

itmMail.Subject = "Per your request"
because this property is the default for all Outlook items.

Copyright © 1999, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners.