Automating Outlook with Visual Basic

Nothing like having your app generate automated e-mail notices to impress people. It’s easy when you use Outlook and Visual Basic. These steps guide you through the process.

Published: Sep 4, 2001
Updated: Jun 8, 2007
6 minute read
We may earn from vendors via affiliate links or sponsorships. This might affect product placement on our site, but not the content of our reviews. See our Terms of Use for details.

Given the popularity of Outlook in today’s work environment, Visual Basic developers are likely to be called upon to automate various functions, such as sending e-mails, creating tasks and notes, adding people to contact lists, and performing other tasks. For example, an application for gathering project information might use VB with Outlook to send e-mail notification to all people involved with the project. Or you might design it to notify management whenever the status of the project changes.

Automating Outlook is easy once you understand Outlook’s object model. In this article, we’ll examine the object model and look at the objects and events that Outlook items support. Then, we’ll create a sample VB app to see they fit together.

Outlook’s object model
The Outlook 2000 object model includes a global Application object. This is the root object that contains the NameSpace object. The NameSpace object contains MAPIFolder objects that represent all folders in a given data source. These MAPIFolder objects contain other objects that represent the items in the data source. An Explorer object is associated with each folder and an Inspector object is associated with each object.

The Application object provides access to all the other objects in Outlook’s object model. The Application object’s CreateItem method allows creating new items and gives you access to Explorer and Inspector objects that represent the Outlook interface. The Application object is the only object in Outlook that can be returned by using CreateObject or GetObject.

The NameSpace object represents a data source, such as MAPI message store. The NameSpace object allows logging in and out of Outlook, returning objects by ID and returning default folders, and gaining access to the data sources owned by other users.

The Folders collection contains all the MAPIFolder objects in the specified message store. The Items collection of a MAPIFolder object contains the objects that represent all Outlook items in a specified folder. The Count property of the Items collection returns 0 when there are no items in a folder. The Items collection contains all the Outlook items in a specified folder. The Inspector object represents the window in which an Outlook item is displayed, and the Explorer item represents the window in which the contents of the folder are displayed.

Working with Outlook folders
Outlook allows you to add folders, add contents to folders, and move and copy items from one folder to another. When manipulating Outlook’s folders, remember that the folder names are case-sensitive. You use the Add method to add the folder to the Folders collection.

Advertisement

Table A shows the default folders that are created when you open Outlook for the first time.
Table A

FolderDescription
InboxThe container for MailItem objects
OutboxThe storage area for items that are completed but not sent yet
Sent MailThe storage area into which copies of user-generated MailItem objects are moved when they are sent
ContactsThe container for ContactItem objects
CalendarThe container for AppointmentItem objects
JournalThe container for JournalItem objects
TasksThe container for TaskItem objects
Deleted ItemsThe storage area into which all objects are moved when they are marked for deletion

Outlook folders and their descriptions

Working with Outlook items and events
The fundamental objects in Outlook include mail messages, appointments, tasks, contacts, journal entries, posts, mail delivery reports, remote mail items, and notes, among others. These objects and their descriptions are summarized in Table B.
Table B

Object
Folder
Description
MailItemInbox folderRepresents a mail message
ContactItemContacts folderRepresents any contact person
JournalItemJournal folderRepresents a journal entry record
NoteItemNotes folderRepresents a note
AppointmentItemCalendar folderIt can represent either a one-time or recurring appointment or meeting. If its MeetingStatus property is set to olMeeting and one or more resources such as attendees or locations are specified, an appointment (AppointmentItem) becomes a meeting (MeetingRequestItem).
MeetingRequestItemRecipient’s Calendar folderThis object is created automatically when you set the MeetingStatus property of AppointmentItem object to olMeeting and send it to one or more users.
TaskItemTasks folderThis represents a task. Tasks can be delegated by assigning them to one or more delegates using the Assign method.
TaskRequestItemRecipient’s Task folderThe item is created automatically when you supply the Assign method to a TaskItem object to assign the task to another user.
ReportItemInbox folderRepresents a mail-delivery report
PostItemPublic folderRepresents a post in a public folder that other users can read
RemoteItemInbox folderThis represents a remote item in the Inbox folder. It’s similar to the MailItem but contains only Subject, Received, Date, Time, Sender, and Size.
DistListItemContacts folderRepresents a distribution list
Link
Represents an item that is linked to another Outlook item
LinksCollection
Contains Link objects representing all items linked to a particular form
SelectionCollection
Contains a set of Outlook items representing the items currently selected in an explorer window
ExplorerCollection
This contains Explorer objects representing all explorer windows. Using this collection, you can access every Outlook explorer window and create new explorer windows.
InspectorsCollection
This contains Inspector objects representing all inspector windows. You can create and access all Outlook inspector windows.

Fundamental objects and collections

Objects and events supported by Outlook items
Now that we’ve reviewed Outlook’s fundamental objects and folders, let’s look at Outlook items. Table C shows the objects supported by Outlook items, and Table D shows the basic events associated with the items.
Table C

ObjectDescription
ActionsActions that can be performed on an item
AttachmentsLinked or embedded objects contained within an item
RecipientUsers to whom the item is addressed
RecurrencePatternPattern of occurrence of a repetitive task or appointment

Objects supported by Outlook items
Table D

EventDescription
OpenOccurs when the inspector for an item is opened
CloseOccurs when the inspector associated with an item is closed
ReadOccurs when an existing item is opened for editing
WriteOccurs every time an item is saved
SendOccurs when the user sends an item
ForwardOccurs when the user forwards an item
ReplyOccurs when the user replies to the item’s sender
ReplyAllOccurs when the user replies to the item’s sender and recipients
ClickOccurs when the user clicks a form control
PropertyChangeOccurs when one of the item’s standard properties is changed
CustomPropertyChangeOccurs when one of the item’s custom properities is modified
CustomAction EventOccurs when on of the item’s custom actions is executed
ActivateOccurs when a particular explorer or inspector window becomes active
AttachmentAddOccurs when an attachment is added to an item
AttachmentReadOccurs when an attachment to an item is opened
FolderAddOccurs when a new folder is added to a folder collection
FolderChangeOccurs when an existing folder is changed
FolderRemoveOccurs when a folder is removed
ItemAddOccurs when an item is added to a collection
ItemChangeOccurs when an item is changed
ItemRemoveOccurs when an item is removed
ItemSendOccurs when an item is about to be sent
NewMailOccurs when a new item or items are received in the user’s Inbox
QuitOccurs when Outlook is about to be closed
ReminderOccurs when a reminder is about to be displayed
StartupOccurs when Outlook is starting
Advertisement

Basic events supported by Outlook items

Time to code
There is nothing like learning by doing, so let’s create a simple Outlook automation project.

  1. ·        Fire up VB and start a new project.
  2. ·        Go to Project References and check Microsoft Outlook Object Library (see Figure A).
Figure A
Checking the Microsoft Outlook Object Library
  1. ·        Double-click on a form.
  2. ·        Add the code in Listing A to the Form_Load Event.
  3. ·        Modify the code where you see anybody@aol.com toreflect your e-mail address.
  4. ·        Press [Ctrl][F5] to run the project, and you should receive an e-mail at the address you specified.

You’ve got mail
From our simple example, you can see that it is a relatively easy matter to add e-mail functionality to your Visual Basic application. But it gets even better. Next time, we’ll take a look at more code for sending e-mails and creating contacts, meetings, notes, and journal entries, as well as for automating other Outlook features from Visual Basic.


Is Office under your control?

What types of office functions have you implemented from VB? What tips do you have for others trying to do the same thing? Send us an e-mail with your suggestions and experiences  or post a comment below.