The Outlook object model is complemented by another object model, called Collaboration Data Objects (CDO). This section briefly discusses this object model and the reasons you might have to use it. CDO provides some functionality unavailable in the Outlook object model. CDO works against the underlying data that Outlook is connected to rather than working against UI elements specific to Outlook. CDO exposes some properties of folders and Outlook items that the Outlook object model does not expose. CDO also provides methods unavailable in the Outlook object model. For example:
The connection between the Outlook object model and CDO is that every Outlook item is in an information store represented in Outlook by a root folder in Outlook's Folder List view. An information store can be an Exchange mailbox on a server or a local PST file. Every information store is identified by a StoreID. Within that information store, an Outlook item is identified by an EntryID. So if you can get the StoreID and EntryID associated with an Outlook item via the Outlook object model, you can write CDO code to get to that same Outlook item using the StoreID and EntryID. Before we show some code that illustrates navigating from an Outlook item to a CDO item, let's consider how to add a reference to the CDO object model. Given that you have a project in Visual Studio, right-click the Project node in Solution Explorer; then click the References tab in the Project Properties dialog box and choose Add to add a reference. In the Add Reference dialog box, shown in Figure 9.13, click the COM tab, select the component Microsoft CDO 1.21 Library, and then click the OK button. Figure 9.13. Adding a reference to CDO.
The result of clicking OK in the dialog box shown in Figure 9.13 is that a reference is added to the CDO library. The CDO library is contained in a namespace called MAPI. No pregenerated primary interop assembly (PIA) for the CDO library existsso Visual Studio creates an interop assembly (IA) for the CDO library. Listing 9.4 shows a VSTO Outlook add-in that navigates from an Outlook MailItem to the corresponding CDO Message object. It handles the Inspectors.NewInspector event and displays a dialog box showing the subject using both an Outlook Item object and CDO's Message object. Also illustrated in this code is the use of CDO's root object, called the Session object. In the Startup method, the code creates a new instance of the Session object and then calls the Session.Logon method to initialize the Session object. In the Shutdown method, the code calls Logoff on the Session object to clean it up properly. The GetMessageFromOutlookItem method gets the CDO Message object that corresponds to an Outlook Item object. It gets several property values in a late-bound way. It gets an EntryID and a StoreID, and then uses the GetMessage method on Session to get a CDO Message object. The GetOutlookItemFromMessage takes a CDO Message and gets the corresponding Outlook Item object. It gets an EntryID and StoreID using properties on CDO's Message object. Then it uses the GetItemFromID method on Outlook's NameSpace object to get an Outlook Item object. Listing 9.4. Getting from an Outlook MailItem to a CDO Message Object
Figure 9.14 shows a diagram of the objects in the CDO object model. This book does not cover the CDO object model in any additional depth. Figure 9.14. The CDO object model. |