Working with Outlook Objects

There are two distinct object libraries you should know about when creating Outlook applications: the Microsoft Forms 2.0 object library and the Microsoft Outlook object library. The Microsoft Forms 2.0 object library contains all the built-in visual interface controls for Outlook forms (discussed in Chapter 5) including text boxes, list boxes, and multipage controls. This object library is contained in the file called Fm20.dll. If you've developed Office applications, you'll be familiar with these controls—they are the same controls that you use to create forms in the other Office applications.

The second object library you should know about is the Microsoft Outlook object library. The object library for Outlook 98 is contained in the file Msoutl85.olb. This file contains the objects that you can use to develop custom Outlook solutions. It is not necessary to have a reference to either of these two libraries in your Outlook forms. Outlook automatically references the libraries for you, so you can start taking advantage of their powerful features.

Getting Help with Outlook Objects

You should take a look at some of the documentation provided by Outlook to help you. For Outlook 98, two help files are useful when creating applications: Vbaoutl.hlp and Olform.hlp. These help files are normally stored in Program Files\Microsoft Office\Office. (You can also find these files on the companion CD.) Vbaoutl.hlp includes information about the Outlook object library. Olform.hlp, shown in Figure 6-3, includes information about the Forms object library and also the Outlook object library.

click to view at full size.

Figure 6-3 The Forms object library help file (Olform.hlp).

These help files include not only detailed information about the Outlook objects and controls in the libraries but also sample code that will help you get started with the objects. This documentation is a great reference tool to use in conjunction with this book: the documentation outlines the objects and their properties, methods, and events, and this book shows you how to implement those objects to create complete solutions.

To access the Outlook object help file (Vbaoutl.hlp) in Outlook, open a form in design mode. From the Form menu, select View Code. From the Help menu, select Microsoft Outlook Object Library Help.

The Outlook object library contains many objects. Due to the sheer volume and functionality of these objects, this chapter will not cover them in detail. Instead, I have included a supplement on the companion CD, named "Programming Outlook and Exchange Supplement," that discusses many of the Outlook objects and collections and includes some sample code. The following is a list of the Outlook objects and collections discussed in this supplement:

  • Application object
  • Explorer object
  • Inspector object
  • Pages collection
  • Page object
  • Controls collection
  • Control object
  • NameSpace object
  • AddressLists collection
  • AddressList object
  • AddressEntries collection
  • AddressEntry object
  • Folders collection
  • MAPIFolder object
  • Items collection
  • PostItem object
  • MailItem object
  • ContactItem object
  • AppointmentItem object
  • MeetingItem object
  • TaskItem object
  • Recipients collection
  • Recipient object
  • UserProperties collection
  • UserProperty object
  • FormDescription object

The Outlook Object Browser

To make it easy for you to find objects in the Outlook object model, Outlook provides an object browser, which is shown in Figure 6-4. The Outlook Object Browser lists the available Outlook objects with their methods and properties. You can quickly add these objects to your code by clicking the Insert button. Clicking Object Help opens the Outlook object library help file (Vbaoutl.hlp).

Figure 6-4 The Outlook Object Browser is accessible from the Script Editor. The object browser allows you to insert objects into your code as well as get help on all the objects.

While Outlook does provide an object browser for the Outlook object library, it does not provide an object browser for the Microsoft Forms 2.0 object library. To browse the objects contained in this library, you need to use an object browser from another product. Since VBA is integrated into the Office products, you can use the VBA Object Browser. And because the Microsoft Forms 2.0 object library is shared across the Office products, you do not need to add a reference to this library in the object browser: the library is added by default to the VBA Object Browser. The following steps explain how to view the Microsoft Forms 2.0 object library from Microsoft Excel 97. The same steps could be used in Microsoft Word 97 or Microsoft PowerPoint 97.

  1. From the Tools menu in Excel 97, select Macro and then Visual Basic Editor.
  2. From the View menu, select Object Browser. The VBA Object Browser is displayed.
  3. To view the Microsoft Forms 2.0 object library, select MSForms from the Project/Library drop-down list, as shown in Figure 6-5.
  4. click to view at full size.

    Figure 6-5 The VBA Object Browser being used from Excel 97 to view the Microsoft Forms 2.0 object library.

  5. To view other object libraries, such as the Outlook object library, you need to add a reference to the library: from the Visual Basic Editor Tools menu, select References. Check the library that you want to add as a reference. For Outlook, check Microsoft Outlook 98 Object Model and click OK.
  6. From the Project/Library drop-down list, select Outlook to view only the Outlook object library.

The Outlook Object Hierarchy

Let's look briefly at the Outlook object hierarchy so that you gain a basic understanding of how these objects can be used. In Chapter 7, we will bring a lot of the concepts we have learned together by looking at an Account Tracking application.

The Outlook object library is a hierarchy of unique objects, as shown in Figure 6-6. This hierarchy makes it easier to understand the object library. To create or edit certain instances of the objects, you need to traverse the hierarchy.

In the Outlook object library, user interface objects are separated from data objects. This allows you to change the controls on your forms without having to modify the underlying data and gives you great flexibility in controlling the user interface presented to your users.

click to view at full size.

Figure 6-6 The Outlook object hierarchy. Notice how the objects are arranged in the hierarchy.

The Outlook object library is also built on the notion of items and collections. An item is a distinct object, such as the ContactItem object, the TaskItem object, and the PostItem object. A collection is a group of related objects. For example, the Items collection is a container for Outlook items.

In Outlook, you can access the specific items in a collection in two ways. The first way is to use the Items collection with an index that contains the specific item you want to access. The following code snippet illustrates this approach. It shows how you can use the Items collection to retrieve the items in your Inbox and display the message text:

 Sub CommandButton1_Click    'Open the Inbox using the GetDefaultFolder method    Set oInbox = Application.GetNameSpace("MAPI").GetDefaultFolder(6)    Set oItems = oInbox.Items    'Notice how you can get the count    Msgbox "Number of items in your Inbox: " & oItems.Count    For counter = 1 to 3       Msgbox oItems(counter).Subject    Next End Sub 

The second way you can access specific items in a collection is by using a named argument. For example, instead of calling the Items collection with an index, you can just pass in a name that corresponds to the default property for the item and uniquely identifies the item. For example, the Subject property is the default property for the MailItem object. Therefore, you can pass in a unique subject name to identify a specific message. The following code illustrates this:

 Sub CommandButton1_Click     'Open the Inbox using the GetDefaultFolder method     Set oInbox = Application.GetNameSpace("MAPI").GetDefaultFolder(6)     'Point to the Inbox Items collection     Set oItems = oInbox.Items     'Display the body of the message that has the Subject     'My Unique Subject     Msgbox oItems("My Unique Subject").Body End Sub 

The topmost object in the Outlook object library is the Application object. All other Outlook objects are created either directly from the Application object or from a child object of the Application object. The Application object provides four key functions that you can take advantage of in your applications. First, the Application object provides one-step creation of any of the built-in Outlook types, such as the Contact, Task, or Message item. Second, the Application object allows you to obtain the currently active user interface elements, such as the current Outlook window the user is displaying and the current Outlook window that contains the folder the user is viewing. Third, the Application object provides an entry point for you to access the data that is stored inside of Exchange Server by using the Outlook objects. Fourth, the Application object, since it is the topmost object, enables you to create or reference the other Outlook objects in your application. When you write code, you'll find that you will use the Outlook Application object extensively.

The code you write in Outlook is automatically passed both a precreated Application object and an Item object. The Application object corresponds to the currently active instance of Outlook, and the Item object corresponds to the currently active instance of the form. This frees you from having to write code that creates both of these objects in your application. In fact, it is strongly recommended that you do not attempt to use the CreateObject function in Outlook to create another instance of Outlook. Instead, you should use the Application object that is available in your VBScript code.

Since the Application and Item objects are already created for your application, you can use their methods and properties immediately. For example, you can change the subject or body of the currently displayed item without having to search the current folder for the item, as shown in the following code:

 Sub CommandButton1_Click     Item.Subject = "This is using the built-in Item object"     Item.Body = "This can make writing code easier"     Importance = 2     'High     'Notice how you do not have to include the Item keyword.     'However, it's a good practice to include the explicit Item      'keyword to make your code more readable. End Sub 

NOTE
This implicit Item object and its methods and properties correspond to the type of Outlook item the form is based on. For example, the properties and methods for an implicit Item in a Contact form are different from those for a Message form. You need to be aware of this when developing your applications.

As you saw with the Item object, the Application object is also available to you automatically in your VBScript code. The following code shows you how to use some of the properties of the built-in Application object. Note that unlike the Item object, the built-in Application object requires you to explicitly place the word Application before any of its methods or properties.

 Sub CommandButton1_Click     Msgbox Application.Version     Application.Quit    'Quits Outlook End Sub 



Programming Microsoft Outlook and Microsoft Exchange
Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)
ISBN: 0735610193
EAN: 2147483647
Year: 1999
Pages: 101

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net