Creating a New Address Book Entry

Creating a New Address Book Entry

You can create new address entries in a collection with the CDO Library version 1.1 and later.

You need permission to Add a new entry to an address book container. Usually you only have this permission for your personal address book (PAB).

For address entries in an address book container, the hierarchy of objects is as follows:

Session object     AddressLists collection         AddressList object             AddressEntries collection                 AddressEntry object                     Fields collection                         Field object

The procedure is basically to work down the hierarchy. After a session is established and logged on, you use the Session object’s AddressLists property to obtain the AddressLists collection, select the AddressList object corresponding to the desired address book container, and use the address list’s AddressEntries property to call the AddressEntries collection’s Add method.

If you have not specified all the parameters in the call to the Add method, you can then supply the missing values by setting AddressEntry object properties such as Address, Name, and Type. You can also set MAPI properties and custom properties using the new address entry’s Fields property. To create a custom property you call the Fields collection’s Add method.

Finally, you commit all the new data to the address book container and to the MAPI system by calling the new address entry’s Update method.

This code fragment adds a new entry to a user’s PAB. Note the use of early binding and of default properties. The objects are declared using early binding to force matching of object types, and to distinguish a MAPI session from other types of sessions available to a Microsoft® Visual Basic® program through other object libraries. The Item property is the default property of all collections and so does not need to be specifically referenced in the statements selecting items from the AddressLists and Fields collections.

Important Storing user names and passwords inside source code can lead to security vulnerabilities in your software. Do not store user names and passwords in your production code.

' we assume we have add permission for our PAB
Function AddEntry()

Dim objSession As MAPI.Session   ' Session object
Dim objMyPAB As AddressList      ' personal address book object
Dim objNewEntry As AddressEntry  ' new address entry object
Dim propTag As Long              ' MAPI property tag for new field

On Error GoTo error_olemsg
Set objSession = CreateObject("MAPI.Session")

' log on to session, supplying username and password
' IMPORTANT: Storing user names and passwords inside source code
' can lead to security vulnerabilities in your software. Do not
' store user names and passwords in your production code.
objSession.Logon 'profileName:="MyProfile", _
                 'profilePassword:="my_password"

' get PAB AddressList from AddressLists collection of Session
Set objMyPAB = objSession.AddressLists("Personal Address Book")
If objMyPAB Is Nothing Then
    MsgBox "Invalid PAB from session"
    Exit Function
End If

' add new AddressEntry to AddressEntries collection of AddressList
Set objNewEntry = objMyPAB.AddressEntries.Add("SMTP", "User One")
objNewEntry.Address = "user@example.com"

' set MAPI property in new AddressEntry (don't need to Add it)
propTag = &H3A08001E ' VB4.0: CdoPR_BUSINESS_TELEPHONE_NUMBER
objNewEntry.Fields(propTag) = "+1-206-555-9901"

' add custom property to new AddressEntry and set its value
objNewEntry.Fields.Add "CellularPhone", vbString
objNewEntry.Fields("CellularPhone") = "+1-206-555-9902"

' commit new entry, properties, fields, and values to PAB AddressList
objNewEntry.Update
MsgBox "New address book entry successfully added"
Exit Function

error_olemsg:
MsgBox "Error " & Str(Err) & ": " & Error$(Err)
Exit Function ' so many steps to succeed; just exit on error

End Function
 

See Also

Concepts

Programming Tasks