Special Edition Using Microsoft Office Outlook 2003
Collaboration Data Objects (CDO) is another method you can use to access Outlook and Exchange data. The CDO object model can help you manipulate Messaging Application Programming Interface (MAPI) folders and items through Visual Basic, VBA, or VBScript. CDO can access certain objects that aren't available in the Outlook object model, such as displaying the Address Book dialog box to the user or accessing the Global Address List or other server-based objects. CDO used to be installed by default in Outlook, but now you must perform a custom installation and explicitly choose to install CDO, as shown in Figure 34.5. Figure 34.5. You must choose to install CDO when you install Outlook.
After you install CDO, you can use it in your code much like you use any other object model, such as Word or Excel. To use CDO to display the Address Book dialog box to obtain recipient information for an email message, use code similar to the following: Sub FindAddress() 'Sub to bring up address window for user to select a recipient On Error Resume Next Set oCDOSession = Application.CreateObject("MAPI.Session") oCDOSession.Logon "", "", False, False, 0 'Use an existing Outlook session txtCaption = "Select Recipient" ButtonText = "Add Recipient" if not err then set orecip = oCDOSession.addressbook (Nothing, txtCaption, False, _ True, 1, ButtonText, "", "", 0) end if if not err then Item.To = orecip(1).Name For i = 2 to orecip.Count Item.To = Item.To & ";" & orecip(i).Name Next end if oCDOSession.logoff oCDOSession = Nothing End Sub This code launches the dialog box shown in Figure 34.5. The code controls the title of the dialog box and the button text, shown here as Add Recipient.
CDO code is subject to the same object model restrictions as code that accesses the Outlook object model. NOTE There are many different versions of CDO. The version that should be installed and running on a client machine is CDO 1.21. However, there is CDO for NT (CDONTS), CDO for Windows 2000 (CDOSYS), CDO for Exchange 2000 (CDOEX), and even CDO for SharePoint Portal Server (PKMCDO). For Outlook development purposes, you should only ever need CDO 1.21, which is installed with Outlook. For a good primer on the different versions of CDO, see http://www.cdolive.com/cdo2.htm.
|