How to automate Outlook 2002 from another program

This article was previously published under Q291120
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Outlook 97 version of this article, see 168095.
For a Microsoft Outlook 98 version of this article, see 181202.
For a Microsoft Outlook 2000 version of this article, see 201096.
This article provides an overview of programming Outlook usingAutomation from another program.
More information
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners -

Microsoft Advisory Services -

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:;EN-US;CNTACTMSAutomation allows one program to control another program by either issuing commands or retrieving information programmatically. You can use the code examples in this article in Microsoft Word, Microsoft Excel, Microsoft Visual Basic, or any other program that supports Automation.

Early vs. Late Binding

You can use either "early" or "late" binding to start an Automationsession. Late binding uses either the GetObject or CreateObject function to initialize Outlook. For example, the following code sets an object to the Outlook program, which is the highest level object in the Outlook object model. All Automation code must first define an Outlook.Application object in order to access any of the other Outlook objects below that.
Dim objOL as ObjectSet objOL = CreateObject("Outlook.Application")				
To use early binding, you first need to reference the available Outlookobject library. To do this from Visual Basic (VB) or Visual Basic forApplications, follow these steps:
  1. In the Visual Basic for Applications Editor, on the Tools menu, click References. Or, if you are using Microsoft Visual Basic, on the Project menu, click References.
  2. Click to select the Microsoft Outlook 10.0 Object Library check box, and then click OK.
The object library file is Msoutl.olb and installs to the C:\ProgramFiles\Microsoft Office\Office10 folder by default. Once you reference the Outlook object library, you can use the followingsyntax to start an Outlook session:
Set ol = New Outlook.Application				
Using early binding has two important advantages. First, code using earlybinding typically runs faster than code using late binding (CreateObject/GetObject). Second, because you reference the Outlook object library, you can get online Outlook programming help using the object browser and Help system.

The Outlook Object Model

The Outlook object model allows you to manipulate data stored in Outlook folders. Other features also allow you to manipulate the Outlook Bar, work with selected items in a folder, manipulate both item-level and application-level windows, and modify views. By adding events, your code can respond to many things that occur in Outlook, allowing you to create an event-driven solution.

However, you will find that compared to Word and Excel, there is limited functionality available to control Outlook itself. Many features of Outlook are not exposed or customizable through the Outlook object model. For example, you cannot use the object model to change most of the Options settings on the Tools menu.

NOTE: As a possible workaround to limitations regarding the object model, you can use the CommandBars object provided by Microsoft Office to execute most commands that are assigned to either toolbar buttons or menu commands. For example, you can use the CommandBars object to run the New Call command, on the Dial submenu of the Tools menu, to bring up the New Call dialog box.

Most programming solutions need to interact with the data stored inOutlook. Outlook stores all of its information in Messaging ApplicationProgramming Interface (MAPI) folders. Therefore, after you set an objectvariable to Outlook.Application, you will commonly set a Namespace object to MAPI:
Set ol = New Outlook.ApplicationSet olns = ol.GetNamespace("MAPI")				
Once you set the Namespace object, you are ready to set the next object to a folder within the MAPI Namespace. One common way of doing this is by specifying the Outlook default folders, which are the folders at the same folder level as the Inbox that receives incoming e-mail. The following code will set the objFolder object to the default Contacts folder:
Set ol = New Outlook.ApplicationSet olns = ol.GetNamespace("MAPI")Set objFolder = olns.GetDefaultFolder(olFolderContacts)				
For additional information about referencing other types of folders, click the article number below to view the article in the Microsoft Knowledge Base:
290804 OL2002: Programming Examples for Referencing Items and Folders
Once you are programmatically at the folder that contains the items youwant to either use or create, you can use appropriate code to accomplishyour programming task. See the examples later in the article for some common programming examples.

In addition to accessing Outlook data from another application, you can also have your application become aware of when certain events occur in Outlook. Examples of events are when an item is added to a folder, when the user selects a different item in the folder, or when an Outlook reminder is displayed. For a complete list of available events, see the Microsoft Outlook Visual Basic Reference (Vbaol10.chm). For information on obtaining the help file, see the References section in this article.

For additional information about integrating Outlook events in your application, click the article number below to view the article in the Microsoft Knowledge Base:
291119 OL2002: Using Outlook Events in Another Program

Sample Code for Common Programming Tasks

Example: Create a New Default Task Item

Sub CreateNewDefaultOutlookTask()   Dim ol As Outlook.Application   Dim NewTask As Outlook.TaskItem   ' Set the Application object.   Set ol = New Outlook.Application   ' Create a new standard task.   Set NewTask = ol.CreateItem(olTaskItem)   ' Display the new task form so the user can fill it out.   NewTask.DisplayEnd Sub				

Example: Create a New Contact Using a Custom Form

Sub CreateNewContactFromCustomForm()   Dim ol As Outlook.Application   Dim olns As Outlook.NameSpace   Dim objFolder As Outlook.MAPIFolder   Dim AllContacts As Outlook.Items   Dim NewContact As Outlook.ContactItem   ' Set the Application object.   Set ol = New Outlook.Application   ' Set the Namespace object.   Set olns = ol.GetNamespace("MAPI")   ' Set the default Contacts folder.   Set objFolder = olns.GetDefaultFolder(olFolderContacts)   ' Set objAllContacts equal to the collection of all contacts.   Set AllContacts = objFolder.Items   ' Add a new contact to the AllContacts collection using the   ' "IPM.Contact.MyForm" form.   Set NewContact = AllContacts.Add("IPM.Contact.MyForm")   ' Display the new contact form.   NewContact.DisplayEnd Sub				

Example: Loop Through All the Default Contacts

Sub GetOutlookContacts()   Dim ol As Object   Dim olns As Object   Dim objFolder As Object   Dim objAllContacts As Object   Dim Contact As Object   ' Set the Application object.   Set ol = New Outlook.Application   ' Set the Namespace object.   Set olns = ol.GetNamespace("MAPI")   ' Set the default Contacts folder.   Set objFolder = olns.GetDefaultFolder(olFolderContacts)   ' Set objAllContacts equal to the collection of all contacts.   Set objAllContacts = objFolder.Items   ' Loop through each contact.   For Each Contact In objAllContacts      ' Display the Fullname field for the contact.      MsgBox Contact.FullName   NextEnd Sub				
For additional information about available resources and answersto commonly asked questions about Microsoft Outlook solutions, click the article number below to view the article in the Microsoft Knowledge Base:
287530 OL2002: Questions About Custom Forms and Outlook Solutions
OfficeKBHowTo OutSol OutSol2002
Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

Article ID: 291120 - Last Review: 10/25/2013 23:20:00 - Revision: 1.0

Microsoft Outlook 2002 Standard Edition

  • kbnosurvey kbarchive kbautomation kbhowto KB291120