The Outlook Object Hierarchy


Let's look at the Outlook object hierarchy so you can gain an understanding of how you can use these objects. In Chapter 8, we will bring together a lot of the concepts you've learned when we build an account tracking application.

The Outlook object library is a hierarchy of unique objects, as shown in Figure 6-4. To create or edit certain instances of the objects, you must traverse the hierarchy.

click to expand
Figure 6-4: The Outlook object hierarchy

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

The Outlook object library also includes items and collections. An item is a distinct object, such as the ContactItem object, the TaskItem object, or 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 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 to access a specific item in a collection is by using a named argument. Instead of calling the Items collection with an index, you pass in a name that corresponds to the default property of 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 shows how:

 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 from the Application object or from a child object of the Application object. The Application object provides four key functions that you can implement 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 a reference to the currently active user interface elements, such as the Outlook window the user is displaying and the 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 Exchange Server by using the Outlook objects. Fourth, the Application object, because it is the topmost object, enables you to create or reference the other Outlook objects in your application. When you write code, you'll use the Outlook Application object extensively.

The code you write in Outlook forms is automatically passed an 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, I strongly recommend 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.

Because 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 you develop 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 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 2003
Programming MicrosoftВ® OutlookВ® and Microsoft Exchange 2003, Third Edition (Pro-Developer)
ISBN: 0735614644
EAN: 2147483647
Year: 2003
Pages: 227
Authors: Thomas Rizzo

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