Export (0) Print
Expand All

How to: Create a Contact Item from a vCard file and Save the Item in a Folder

This example imports all the vCard files in a file system folder and saves the contacts into the folder specified by the targetFolder parameter.

This example uses the OpenSharedItem method. The OpenSharedItem method opens messages stored as Outlook message format (.msg) files, iCalendar appointment (.ics) files, or vCard (.vcf) files. Be sure to cast the returned object to the appropriate item type and call the corresponding Save method to persist the item. By default, the item returned by OpenSharedItem is saved in the default folder for the specific item type. You can use the corresponding Move method to move the item to a different folder.

If you use Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void ImportContacts(string path, Outlook.Folder targetFolder)
{
    Outlook.ContactItem contact;
    Outlook.ContactItem moveContact;
    if (Directory.Exists(path))
    {
        string[] files = Directory.GetFiles(path, "*.vcf");
        foreach (string file in files)
        {
            contact = Application.Session.OpenSharedItem(file)
                as Outlook.ContactItem;
            if (targetFolder ==
                Application.Session.GetDefaultFolder(
                Outlook.OlDefaultFolders.olFolderContacts)
                as Outlook.Folder)
            {
                contact.Save();
            }
            else
            {
                moveContact = contact.Move(targetFolder)
                    as Outlook.ContactItem;
                moveContact.Save();
            }
        }
    }
}

Other Resources

Show:
© 2014 Microsoft