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.

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

Folder Description
Inbox The container for MailItem objects
Outbox The storage area for items that are completed but not sent yet
Sent Mail The storage area into which copies of user-generated MailItem objects are moved when they are sent
Contacts The container for ContactItem objects
Calendar The container for AppointmentItem objects
Journal The container for JournalItem objects
Tasks The container for TaskItem objects
Deleted Items The 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
MailItem Inbox folder Represents a mail message
ContactItem Contacts folder Represents any contact person
JournalItem Journal folder Represents a journal entry record
NoteItem Notes folder Represents a note
AppointmentItem Calendar folder It 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).
MeetingRequestItem Recipient’s Calendar folder This object is created automatically when you set the MeetingStatus property of AppointmentItem object to olMeeting and send it to one or more users.
TaskItem Tasks folder This represents a task. Tasks can be delegated by assigning them to one or more delegates using the Assign method.
TaskRequestItem Recipient’s Task folder The item is created automatically when you supply the Assign method to a TaskItem object to assign the task to another user.
ReportItem Inbox folder Represents a mail-delivery report
PostItem Public folder Represents a post in a public folder that other users can read
RemoteItem Inbox folder This represents a remote item in the Inbox folder. It’s similar to the MailItem but contains only Subject, Received, Date, Time, Sender, and Size.
DistListItem Contacts folder Represents 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

Object Description
Actions Actions that can be performed on an item
Attachments Linked or embedded objects contained within an item
Recipient Users to whom the item is addressed
RecurrencePattern Pattern of occurrence of a repetitive task or appointment

Objects supported by Outlook items
Table D

Event Description
Open Occurs when the inspector for an item is opened
Close Occurs when the inspector associated with an item is closed
Read Occurs when an existing item is opened for editing
Write Occurs every time an item is saved
Send Occurs when the user sends an item
Forward Occurs when the user forwards an item
Reply Occurs when the user replies to the item’s sender
ReplyAll Occurs when the user replies to the item’s sender and recipients
Click Occurs when the user clicks a form control
PropertyChange Occurs when one of the item’s standard properties is changed
CustomPropertyChange Occurs when one of the item’s custom properities is modified
CustomAction Event Occurs when on of the item’s custom actions is executed
Activate Occurs when a particular explorer or inspector window becomes active
AttachmentAdd Occurs when an attachment is added to an item
AttachmentRead Occurs when an attachment to an item is opened
FolderAdd Occurs when a new folder is added to a folder collection
FolderChange Occurs when an existing folder is changed
FolderRemove Occurs when a folder is removed
ItemAdd Occurs when an item is added to a collection
ItemChange Occurs when an item is changed
ItemRemove Occurs when an item is removed
ItemSend Occurs when an item is about to be sent
NewMail Occurs when a new item or items are received in the user’s Inbox
Quit Occurs when Outlook is about to be closed
Reminder Occurs when a reminder is about to be displayed
Startup Occurs when Outlook is starting

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.


 

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays