65.9K
CodeProject is changing. Read more.
Home

Working with vCards using the MS CDO for Exchange Library

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.38/5 (5 votes)

Feb 1, 2006

2 min read

viewsIcon

33880

downloadIcon

454

vCard is a popular format developed to exchange business information. It was developed to easily share your contacts with other people. In this article, I will explain how to get personal infromation from a vCard, edit it, and save the changes in a file.

Introduction

vCard is a popular format developed to exchange business information. It was developed to easily share your contacts with other people using email clients, internet, or event portable devices and cell phones. A vCard file includes text information about name, home and business addresses, phone numbers, and other things. It can also include your photo or company’s logo.

I am working on a web project where users can upload vCards into a system to export personal information. I searched for any information about working with vCards, in MSDN, and found a link to the CDO.Person object in the Microsoft CDO For Exchange 2000 Library. This library doesn’t present until you install Exchange Server. But I Googled and found codex.dll separately here. I registered it using the regsvr32 utility and added it to my Visual Studio project.

If you take a look at the IPerson Interface description, you can find such properties like FirstName, LastName, Company etc. There is also a method GetVCardStrem which returns the ADO Stream object containing the vCard stream information.

For example, to create a CDO.Person object from a vCard file, use the following code:

CDO.Person person = new CDO.Person();
ADODB.Stream s = person.GetVCardStream();
s.LoadFromFile(this.vCardFileName);
s.Flush();

Now, you can access all the CDO.Person properties to retrieve the personal information:

txtFirstName.Text = person.FirstName;
txtLastName.Text = person.LastName;
txtEmail.Text = person.Email;
txtHomePhone.Text = person.HomePhone;
txtCellPhone.Text = person.MobilePhone;

Also, you can store a CDO.Person object into a vCard file using the SaveToFile method of ADODB.Stream:

ADODB.Stream s = person.GetVCardStream();
s.SaveToFile(@“c:\vCard.vcf”, 
    ADODB.SaveOptionsEnum.adSaveCreateOverWrite);

The first discomfort of this method is the necessity to register COM on a remote server when you are using this approach for web applications. The second problem is you can not work with included images in a vCard. But this is the easiest way to get the main information about a person's contact information from his vCard when you have the Microsoft CDO For Exchange 2000 Library installed.

I created a Windows application in Visual Studio 2005 using C#, with the example code where you can open a sample vCard file, preview data, change it, and then save it in a file. Before running the project, check whether the Microsoft CDO For Exchange library is registered. If not, download it from here and register cdoex.dll with the regsvr32 utility.