Working with Outlook Objects

[Previous] [Next]

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 4) 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 2000 is contained in the file Msoutl9.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 2000, a help file is useful when creating applications: Vbaoutl09.chm. This help file is normally stored in Program Files\Microsoft Office\Office. (You can also find it on the companion CD.) The help file includes information about the Outlook object library and the Forms object library and is shown in Figure 5-3.

These help files include not only detailed information about the Outlook objects and controls in the libraries but also code samples 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 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
  • MAPIFolder object
  • Explorer object
  • Items collection
  • Inspector object
  • PostItem object
  • Pages collection
  • MailItem object
  • Page object
  • ContactItem object
  • Controls collection
  • AppointmentItem object
  • Control object
  • MeetingItem object
  • NameSpace object
  • TaskItem object
  • AddressLists collection
  • Recipients collection
  • AddressList object
  • Recipient object
  • AddressEntries collection
  • UserProperties collection
  • AddressEntry object
  • UserProperty object
  • Folders collection
  • FormDescription object
  • click to view at full size.

    Figure 5-3. The Outlook help file.

    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 5-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.

    Figure 5-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 the VBA object browser from another product or from within Outlook. 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 within Outlook. The same steps could be used in Microsoft Word 2000 or Microsoft PowerPoint 2000.

    1. From the Tools menu, 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 5-5.

    NOTE
    If you don't see MSForms in your drop-down list, add it to your references, as explained in the next step.

      click to view at full size.

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

    1. 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 9.0 Object Library and click OK.
    2. 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 6, 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 56. 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.

    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 references 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 

    click to view at full size.

    Figure 5-6. The Outlook object hierarchy.

    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 automatically available to you 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: 2000
    Pages: 184

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