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.
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.
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.
Time to code
There is nothing like learning by doing, so let’s create a simple Outlook automation project.
- · Fire up VB and start a new project.
- · Go to Project References and check Microsoft Outlook Object Library (see Figure A).
|Checking the Microsoft Outlook Object Library|
- · Double-click on a form.
- · Add the code in Listing A to the Form_Load Event.
- · Modify the code where you see email@example.com toreflect your e-mail address.
- · 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.