OL97: How to Automate Outlook from Another Program

ID: Q168095

The information in this article applies to:

SUMMARY

This article provides an overview of programming Microsoft Outlook 97 using automation 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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://support.microsoft.com/support/supportnet/refguide/default.asp

Automation (formerly OLE Automation) 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 97, Microsoft Excel 97, 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 automation session. Late binding uses either the GetObject or CreateObject command 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 Object
  Set objOL = CreateObject("Outlook.Application")

To use early binding, you first need to "Reference" the available Outlook Object Library. To do this from Visual Basic (VB) or Visual Basic for Applications, on the Visual Basic Editor Tools menu, click References. Select "Microsoft Outlook 8.0 Object Library" from the list and click OK. The object library file is Msoutl8.olb and by default installs to the C:\Program Files\Microsoft Office\Office folder.

Once you reference the Outlook Object Library, you can use the following syntax to start an Outlook session:

  Set ol = New Outlook.Application

Using early binding has two important advantages. First, code using early binding runs faster than code using late binding (CreateObject/GetObject). Second, because you reference the Outlook Object Library, you can get on- line Outlook programming help using the object browser and Help system.

The Outlook Object Model

The Outlook object model provides all of the functionality necessary to manipulate data stored in Outlook folders. However, there is limited functionality available to control Outlook itself. For example, there is no programming method to change the Options settings on the Tools menu, in Outlook via the object model.

You can use the CommandBars object provided by Microsoft Office to execute commands that are assigned to either toolbar buttons or menu items. For example, you can use the CommandBars object to execute the Tools/Dial/New Call command to bring up the New Call dialog box.

Most programming solutions need to interact with the data stored in Outlook. Outlook stores all of it's information in Messaging Application Programming Interface (MAPI) folders. Therefore, after you set an object variable to Outlook.Application, you will commonly set a "Namespace" object to MAPI:

  Set ol = New Outlook.Application
  Set 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 Outlook's "default" folders, which are the folders at the same folder level as the Inbox that receives incoming mail. The following code will set the objFolder object to the default Contacts folder:

  Set ol = New Outlook.Application
  Set olns = ol.GetNamespace("MAPI")
  Set objFolder = olns.GetDefaultFolder(olFolderContacts)

Once you are programmatically at the folder that contains the items you wish to either use or create, you must use appropriate code to accomplish your programming task. See the section below for some common programming examples.

SAMPLE CODE FOR COMMON PROGRAMMING TASKS

Create a New Default Task Item

   Sub CreateNewDefaultOutlookTask()
      Dim ol As Object
      Dim NewTask As Object
      ' Set the application object
      Set ol = New Outlook.Application
      ' You can only use CreateItem for default items
      ' Vbaoutl.hlp lists other Outlook constants to create other items
      Set NewTask = ol.CreateItem(olTaskItem)
      ' Display the new task form so the user can fill it out
      NewTask.Display
   End Sub

Create a New Task Using a Custom Form

   Sub CreateNewContactFromCustomForm()
      Dim ol As Object
      Dim olns As Object
      Dim objFolder As Object
      Dim AllContacts As Object
      Dim NewContact 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 = the collection of all contacts
      Set AllContacts = objFolder.Items
      ' Add a new contact the AllContacts collection using the
      '   "IPM.Contact.MyForm" form
      Set NewContact = AllContacts.Add("IPM.Contact.MyForm")
      ' Display the new contact form
      NewContact.Display
   End Sub

Sample Subroutine Loops 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 = 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
      Next
   End Sub

Tips for Using the Help File

When you use the Vbaoutl.hlp help file, create a shortcut to the file on your desktop or the Start menu. If you start this Help file from within another program's Visual Basic Editor, you will not see the Help Contents tab. Opening the Help file directly gives you more control when navigating through the contents of the file.

For more information on how to install the Vbaoutl.hlp file, please see the following article in the Microsoft Knowledge Base:

   Article-ID:   Q166738
   Title     :   OL97: How to Install Visual Basic Help

If you're not sure if the Outlook object model supports something you're trying to do, open the Vbaoutl.hlp help file and click the Find tab and search for some keywords related to what you are trying to accomplish. If you do not find a help topic, it is safe to assume the object model does not support that feature.

Resources for Outlook Automation

There are many resources for learning how to program using Outlook. For more information on available Outlook programming resources, please see the following article in the Microsoft Knowledge Base:

   Article-ID:   Q166368
   Title     :   OL97: How To Get Help Programming with Outlook

The following list includes some important resources for automating Outlook, located on the Microsoft Outlook Developer Forum:

REFERENCES

For more information about creating solutions with Microsoft Outlook 97, please see the following articles in the Microsoft Knowledge Base:

   Article-ID: Q166368
   Title     : OL97: How to Get Help Programming with Outlook

   Article-ID: Q170783
   Title     : OL97: Q&A: Questions about Customizing or
               Programming Outlook

Additional query words: OutSol OutSol97

Keywords          : kbcode kbprg 
Version           : 97
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999