Click here to Skip to main content
15,891,943 members
Articles / Programming Languages / C#
Article

Working with vCards using the MS CDO for Exchange Library

Rate me:
Please Sign up or sign in to vote.
4.38/5 (5 votes)
1 Feb 20062 min read 33.6K   454   21   2
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:

C#
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:

C#
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:

C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Ukraine Ukraine
Alexander is freelance web developer with 4 years experience. He has skills in ASP.NET/MS SQL Server, PHP/MySQL, Ruby On Rails, XHTML, CSS. You can contact with me by seigo.ua@gmail.com. Also check my homepage at www.klalex.com.

Comments and Discussions

 
Generaldoesn't have Person Pin
msn_issue5-Jun-10 9:53
msn_issue5-Jun-10 9:53 
GeneralRe: doesn't have Person Pin
Asthana Pratik18-Jul-10 19:46
Asthana Pratik18-Jul-10 19:46 
Hi,
After register cdoex.dll using regsvr32 ,add the dll to project using add refrence option .

Then it will work fine for you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.