Creating a Contact

Because contacts are stored as message items in the MAPI system, creating a contact is just like creating a new message. The steps to create a new contact are:

  1. Obtain a reference to the Messages collection of a contacts folder.

  2. Invoke the Messages collection’s Add method to create a new contact item as a Message object.

  3. (Optional) Set properties of the new contact item.

  4. Invoke the Message object’s Update method to save the contact in the message store.

Example 10-6 shows how to create a new contact, setting some additional properties at the same time. This code makes use of the LetMAPIProperty subroutine shown in Example 10-4. Figure 10-1 shows the newly created contact, as displayed by Microsoft Outlook 98.

Tip

As was the case with task items, some contact item properties are inaccessible. One of these properties is the “file as” setting in Outlook. This is the setting that labels each contact in Outlook’s Address Card view. Therefore, contacts created programmatically appear to have no name when looked at in this view. CDO does not provide a workaround for this.

Example 10-6. Creating a Contact

' gCdoSession previously Dim'ed, Set, and logged on. Dim CdoFolder As MAPI.Folder Dim CdoMessages As MAPI.Messages Dim CdoMessage As MAPI.Message ' Get the Contacts folder. Set CdoFolder = gCdoSession.GetDefaultFolder(CdoDefaultFolderContacts) ' Get the collection of Contacts. Set CdoMessages = CdoFolder.Messages ' Add a new Contact. Set CdoMessage = CdoMessages.Add(Subject:="Jane ...

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.