In the June 2000 Outlook VBA on Demand column, I showed you how to use VBA in Outlook 2000 to display, for a message in your Inbox folder, the sender's email address. In this installment, I show you how to build a small application that loops through any folder containing contacts and updates the items so that an open contact's (or folder view's) email fields display the Internet email address—not the name of the contact.
Outlook uses two separate properties—one for the address and one for a display name—for each of a contact's three email fields. In the contact item and the contacts folder view, you always see the display name. Depending on how you create the contact, the display name might default to the contact's FullName property. You might think that you can simply update the Email1DisplayName property with the value in the Email1Address property, but you can't. The Email1DisplayName property is read-only. The only way you can update it is to completely recreate the email address entry—and that is what this column's application does.
Create a new module in Outlook VBA (or use an existing module in which you've been creating macros), and add a new subroutine that uses the code you see in Listing 1. The subroutine uses the PickFolder method on the Namespace object to let the user select the folder to update; then, the subroutine returns the folder's collection of items as the colContacts object variable and returns the first item in the folder. Because Outlook 2000 contacts folders can contain distribution lists (DLs) as well as contacts, the Dim objItem As Object statement declares the object that represents an item in the folder as a generic Object, not as a ContactItem object. When working in a contacts folder, you should always declare the object variable that represents an individual item as a generic Object, then use the object variable's Class property to check the actual type as you loop through the items in the folder:
If objItem.Class = olContact Then
You could also use the TypeName() function, if you prefer. After you've ensured that the item is a ContactItem, you can access its properties and make your changes. To minimize the amount of work the procedure must perform, the subroutine checks to see whether the item's first address is an Internet (i.e., SMTP) address and whether the item's display name and address are different:
If the address is an Internet address and the two properties are different, the subroutine copies the address to a string variable, sets the Email1Address property to an empty string, then sets it again—this time to the original address:
.Email1Address = ""
.Email1Address = strAddress
The subroutine repeats this procedure for the Email2Address and Email3Address properties. If any of the properties required changes, the Saved property returns True, and the code saves the item.
Notice the structure that begins with the With objItem statement and finishes with the End With statement. Inside this structure, you can refer to any property or method of the item by simply prefixing the property or method with a period (e.g., .Email1Address instead of objItem.Email1Address). This tip will make your code easier to follow.
You can run the ConvertEmailDisplayToAddress procedure any time users complain that they can't see email addresses in a contacts folder. The procedure works with any contacts folder that you can see in your folder list.