Automating Outlook from a Visual Basic Application
|Outlook Developer Reference|
Because Microsoft Outlook supports Automation, you can control Outlook from any program that is written with Microsoft Visual Basic. Automation provides a standard method for one application to access the objects, methods, properties, and events of other applications that support Automation.
The Outlook object model provides all of the functionality necessary to manipulate data that is stored in Outlook folders, and it provides the ability to control many aspects of the Outlook user interface (UI).
To start an Outlook Automation session, you can use either early or late binding. Late binding uses either the Visual Basic GetObject function or the CreateObject function to initialize Outlook. For example, the following code sets an object variable to the Outlook Application object, which is the highest-level object in the Outlook object model. All Automation code must first define an Outlook Application object to be able to access any other Outlook objects.
To use early binding, you first need to set a reference to the Outlook object library. Use the Reference command on the Visual Basic for Applications (VBA) Tools menu to set a reference to Microsoft Outlook xx.x Object Library, where xx.x represents the version of Outlook that you are working with. You can then use the following syntax to start an Outlook session.
Most programming solutions interact with the data stored in Outlook. Outlook stores all of its information as items in folders. Folders are contained in one or more stores. After you set an object variable to the Outlook Application object, you will commonly set a NameSpace object to refer to MAPI, as shown in the following example.
Once you have set an object variable to reference the folder that contains the items you wish to work with, you use appropriate code to accomplish your task, as shown in the following example.
If you are using VBA to create macros, there are two ways you can automate Outlook. You can implement a macro that creates a new instance of the Outlook Application object. The
CreateNewDefaultOutlookTask() method above shows how to call
New Outlook.Application to create a new Application object instance.
|This new instance of Outlook is not trusted and can trigger the Object Model Guard. For more information about the Object Model Guard, see What's New for Developers in Outlook 2007 (Part 1 of 2).|
As an alternative to creating and automating a separate instance of Outlook, you can use VBA to implement a macro that automates the current instance of Outlook. To do so, use the Application object intrinsic to the environment. This Application object is trusted and can avoid triggering the Object Model Guard. For more information about the object model guard, see What's New for Developers in Outlook 2007 (Part 1 of 2). The following example shows the
CreateAnotherNewDefaultOutlookTask() method using the Application object from the current instance of Outlook.