Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


February 19, 2002

Using Collaboration Data Objects


RSS
Subscribe to Windows IT Pro | See More CDO Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

CDO lets you show an SMTP address for Exchange email senders

In "Showing the Sender's Email Address," June 2000, InstantDoc ID 8630, I demonstrated how to use Outlook VBA code to monitor your Inbox folder and, for each new item, display the sender's actual email address—not just a display name—in the folder. However, for users in the same Microsoft Exchange Server 5.5 organization, this technique shows the internal X.400 address instead of an easy-to-read SMTP address.

To display an SMTP address for Exchange senders as well as external senders, you need Collaboration Data Objects. CDO, an alternative programming interface that you can use with Outlook, provides a way to obtain many properties that the Outlook object model doesn't expose.

Preliminaries
Neither Outlook 2002 nor Outlook 2000 installs CDO by default. Before working with this article's code, determine whether the CDO component is present on your system and install it if necessary. You can use the Control Panel Add/Remove Programs applet to launch the Outlook or Office maintenance dialog box and follow the wizard to check your configuration. Look among the Outlook components for Collaboration Data Objects. You'll also need to use Tools, References in Outlook VBA to add the Microsoft CDO 1.21 library to your project.

Put the code in Listing 1 in the Outlook VBA window's ThisOutlookSession module. If you have other code that you want to run against new messages in the Inbox, include it in the colInboxItems_ItemAdd procedure, inside the If Item.Class = olMail Then ... End If block.

Restart Outlook and send yourself a message to test the code. One important note: Because the code accesses the address book, the code is subject to the controls of the Outlook E-mail Security Update. Specifically, for users who have Outlook 2000 with the security update or who have Outlook 2002, the code will display a prompt to which the user must respond before the code can proceed, unless the Exchange administrator allows unrestricted programmatic access to the address book.

How It Works
The colInboxItems_ItemAdd procedure uses the UserProperties.Add() method to add both the FromAddress and SenderPhone properties to the incoming message. You can use the Field Chooser to add these fields to the folder view. Right-click the Inbox's column headings and select Field Chooser. Select User-defined fields in Inbox, and drag the new fields to the column headings.

The code included in "Showing the Sender's Email Address" used the CDO Message.Sender.Address property to get the sender's address. The more sophisticated version here uses the Fields collection to gather two additional pieces of information from the AddressEntry object called Sender: the type of address and, for senders with Exchange addresses, the default SMTP address.

The Fields collection is simple to use. The expression objAE.Fields(CdoPR_ADDRTYPE) returns the address type (e.g., EX—Exchange Server, SMTP) from the objAE AddressEntry object. The hard part is knowing what to use as the argument for Fields. Here, I use a constant exposed by the CDO 1.21 library. The Microsoft Developer Network (MSDN) Web site provides a list of these property tags, as they're called, at http://msdn.microsoft.com/library/en-us/cdo/html/_olemsg_mapi_property_tags.asp. A tool such as mdbvu32.exe on the Exchange CD-ROM or OutlookSpy from http://www.dimastr.com is invaluable in understanding which property contains what information.

However, not all fields are directly available through constants. In these cases, if you know the field's property tag, you can declare it as a constant to make your code more readable. The field for the SMTP address is a good example. Listing 1 includes a constant declaration for CdoPR_EMAIL, whose property tag value is &H39FE001E.

To get the sender's phone number, the code uses another CDO property tag—CdoPR_OFFICE_TELEPHONE_NUMBER—to get the main telephone number from an Exchange user's Global Address List (GAL) entry. If the sender has an SMTP address rather than an Exchange address, the code uses the Find method to locate the first matching contact with that address in the Contacts folder.

More CDO
If you want to explore CDO further, the CDOLive Web site (http://www.cdolive.com/cdo10.htm) provides more documentation about how to use the CDO Fields collection and different kinds of property tags. It even shows how to use CDO to access Outlook item properties.

End of Article



Reader Comments
I get an error on this line.

Set objCDOItem = objSession.GetMessage(strStoreID, strEntryID)

Has anyone else?

Thanx

Eric August 28, 2002


Nothing appears in "User Defined Fields" list. Here's what I did:
- Verified install of CDO
- Added CDO to the default (and only) project in the VBEditor
- Put the code in Listing 1 in the ThisOutlookSession module.
(commented 1st line, "'LISTING 1:..")
- Quit Outlook. Verified that process Outlook.exe was gone.
- Started Outlook, Sent myself an email from elsewhere
- Received the email in Outlook POP3 Inbox
- Customize Current View:Fields...:User-Defined Fields
List of user-defined fields was blank.
How does the Subroutine colInboxItems_ItemAdd() get called? Do I need to call it somewhere to get it to work?
Thanks


Monte December 08, 2003


Wouldn't have just been easier if M$ would have included a darn email address only column!? Seriously, how dumb are these guys to leave that out!?!?!?

Annoyed with M$ May 17, 2004


Monte, ItemAdd is an event handler. It runs when a new item is added to the Items collection represented by colInboxItems.

Sue_Mosher August 19, 2004 (Article Rating: )


Check out this great tutorial on using Microsoft Outlook for managing your ebay & amazon sales, or use it as an Outlook programming quickstart:

http://www.321books.co.uk/ebooks/outlook-vba-tutorial.htm



mal4mac September 17, 2004 (Article Rating: )


thanks, I had a different situation where I just needed to return the actual email addy (just needed to know that &H39FE001E was the email address) and had to search around a lot; this article finally shed some light. thanks again!

Anonymous User November 15, 2004 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Microsoft, News Corp. Discuss Locking Out Google

Microsoft and Rupert Murdoch's News Corp. recently discussed an alliance that would counter Google's fledgling online news service. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Exchange Server and Outlook Whitepapers Email Controls and Regulatory Compliance

Take Control of Your Email: Understand the Business Reasons for Email Storage Management

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Bail Out Your Exchange Environment

Check out our list of Free Email Newsletters!

Exchange Server and Outlook eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

The Expert's Guide for Exchange 2003: Preparing for, Moving to, and Supporting Exchange Server 2003

Related Exchange Server and Outlook Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

Exchange & Outlook UPDATE eNewsletter
News, strategies, products, and developments in Exchange Server and Outlook messaging.

Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement