Share via


Working with the Outlook Object Model

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The programming model of Microsoft® Outlook® differs somewhat from the other Microsoft® Office applications. You can work with Outlook's object model in three ways:

  • You can write Microsoft® Visual Basic® for Applications (VBA) code that runs from a local project file or a COM add-in that is associated with the local installation of Outlook.
  • You can use the native scripting environment available within the Outlook forms that are used to display items such as messages and appointments.
  • You can use automation to work with Outlook from other Office applications and applications that support VBA.

To write VBA code that runs from a local project file (VBAProject.OTM), open Outlook, point to Macro on the Tools menu, and then click Visual Basic Editor. In Outlook, the Visual Basic Editor makes it possible for you to write code that can be run from this installation of Outlook only. For example, by adding code to the ThisOutlookSession module, you can write code against the following Application object events: ItemSend, NewMail, OptionsPagesAdd, Quit, Reminder, and Startup. Just as with other Office applications, you can insert code modules, class modules, and UserForms to further customize your solution, and you can run procedures from menu items or toolbar buttons.

To distribute a solution created by using a local Outlook VBA project, you can export your modules and objects to files and then import them on other machines where Outlook is installed. However, a much better way to distribute your solution is to compile and install your solution as a COM add-in by using the COM add-in designer available in Microsoft® Office XP Developer or in Microsoft® Visual Basic® 6.0.

To write script that runs within Outlook items, you use the Outlook Script Editor. Outlook forms (that is, all items you can open in Outlook, such as messages, appointments, and contacts) support scripting in Microsoft® Visual Basic® Scripting Edition (VBScript) by using the Outlook Script Editor. Because VBScript is a subset of VBA, there are limitations to what you can do; for example, VBScript supports only one data type, the Variant data type, and a number of VBA keywords and features aren't supported. To access the Outlook Script Editor, you must be in form design mode.

To open the Outlook Script Editor

  1. Open the Outlook item you want to base your form on.
  2. On the Tools menu, point to Forms, and then click Design This Form. To start from an existing custom form, point to Forms on the Tools menu, click Design a Form, and then select a form.
  3. Make any changes you want to the design of the form.
  4. On the Form menu, click View Code.

When working with scripting in Outlook forms, you will most typically be writing event procedures. For example, you might want to write an event procedure for your form's Open event to initialize the form to display a particular tab page and enter default values in certain fields.

To add an event handler stub to the Outlook Script Editor

  1. From the Script menu, click Event Handler.
  2. Select the event you want to work with, and then click Add.

For more information about the Outlook object model, click Microsoft® Outlook® Object Library Help on the Help menu in the Outlook Script Editor.

To work with Outlook from another application, you can use automation code with either early binding or late binding. To use early binding, establish a reference to the Microsoft® Outlook® object library and then declare and initialize an object variable that references the Outlook Application object. For example:

Dim olApp As Outlook.Application
Set olApp = New Outlook.Application

Similarly, you can use the CreateObject function with an object variable declared as type Object to initialize a late-bound object variable. For example:

Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")

Either way, you can then use this object variable to work with the other objects, properties, methods, and events of the Outlook object model.

See Also

Objects, Collections, and Object Models: Technology Backgrounder | Objects Exposed by an Object Model | Collections | Properties and Methods | Events | Using the Object Browser | Add-ins, Templates, Wizards, and Libraries | Working with Microsoft Outlook Objects | Using Web Technologies with Office XP