Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath

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:

  • CDO lets you delete an Outlook item permanently without first routing it to the Deleted Items folder, whereas Outlook always routes Outlook items you delete to the Deleted Items folder.

  • CDO lets you programmatically show the Select Names dialog box, which can be used to choose recipients for an e-mail message.

  • CDO lets you read and write several properties that are either not available in the Outlook object model or are read-only in the Outlook object model.

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

Imports Outlook = Microsoft.Office.Interop.Outlook Public Class ThisApplication Private nameSpace1 As Outlook.NameSpace Private mapiSession As MAPI.Session Private inspectors1 As Outlook.Inspectors Private Sub ThisApplication_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup nameSpace1 = Me.Session mapiSession = New MAPI.Session() mapiSession.Logon(ShowDialog:=False, NewSession:=False) inspectors1 = Me.Inspectors AddHandler inspectors1.NewInspector, _ AddressOf Inspectors_NewInspector End Sub Private Sub ThisApplication_Shutdown(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Shutdown mapiSession.Logoff() End Sub Private Function GetMessageFromOutlookItem( _ ByVal outlookItem As Object) As MAPI.Message ' Late Bound Properties Dim entryID As Object = outlookItem.EntryID Dim parentFolder As Object = outlookItem.Parent Dim storeID As Object = parentFolder.StoreID Return CType(mapiSession.GetMessage(entryID, storeID), MAPI.Message) End Function Private Function GetOutlookItemFromMessage( _ ByVal message As MAPI.Message) As Object Dim entryID As String = CType(message.ID, String) Dim storeID As String = CType(message.StoreID, String) Return nameSpace1.GetItemFromID(entryID, storeID) End Function Private Sub Inspectors_NewInspector( _ ByVal inspector As Outlook.Inspector) Dim inspectedItem As Object = inspector.CurrentItem Dim message As MAPI.Message = _ GetMessageFromOutlookItem(inspectedItem) MsgBox(String.Format("message.Subject={0}", message.Subject)) Dim outlookItem As Object outlookItem = GetOutlookItemFromMessage(message) MsgBox(String.Format( _ "outlookItem.Subject={0}", _ outlookItem.Subject)) End Sub End Class

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.

Категории