Search code examples
c#outlookimport-contacts

Problem with importing contact from outlook different version through .net code


I created code for importing contact from outlook. Created application in .net framework with version 4.0 in C#.

code is as follows -

OutLook._Application outlookObj = new OutLook.Application();
outlookObj.ActiveExplorer();
OutLook.MAPIFolder contactsFolder = (OutLook.MAPIFolder)outlookObj.Session
    .GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);
comboDelimiter.Properties.Items.Clear();
if (!comboDelimiter.Properties.Items.Contains("Default"))
{
    comboDelimiter.SelectedText = "Default";
    comboDelimiter.Properties.Items.Add("Default");
}
//VERIFYING THE CUSTOM FOLDER IN OUT LOOK .
foreach (OutLook.MAPIFolder subFolder in contactsFolder.Folders)
{
    if (!comboDelimiter.Properties.Items.Contains(subFolder.Name))
    {
        comboDelimiter.Properties.Items.Add(subFolder.Name);
    }
}

This function is used for reading contacts from the particular folder of the outlook contact -

private DataSet GetContactsFromFolder(string folderName)
{
    object missing = System.Reflection.Missing.Value;
    DataSet ds = new DataSet();
    //create instance of Outlook application and Outlook Contacts folder.
    try
    {
        OutLook.MAPIFolder fldContacts = null;
        OutLook._Application outlookObj = new OutLook.Application();
        if (folderName == "Default")
        {
            fldContacts = (OutLook.MAPIFolder)outlookObj.Session
                .GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);
        }
        else
        {

            OutLook.MAPIFolder contactsFolder = (OutLook.MAPIFolder)outlookObj.Session
                .GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);
            //VERIFYING THE CUSTOM FOLDER IN OUT LOOK .
            foreach (OutLook.MAPIFolder subFolder in contactsFolder.Folders)
            {
                if (subFolder.Name == folderName)
                {
                    fldContacts = subFolder;
                    break;
                }
            }
        }
        DataTable dt = new DataTable();
        for (int i = 0; i < 12; i++)
        {
            dt.Columns.Add("Col" + i,Type.GetType ("System.String"));
        }
        foreach (Microsoft.Office.Interop.Outlook._ContactItem contactItem in fldContacts.Items)
        {
            {
                DataRow dr = dt.NewRow();
                dr[0] = Convert.ToString(contactItem.FirstName);
                dr[1] = Convert.ToString(contactItem.LastName);
                dr[2] = Convert.ToString(contactItem.MobileTelephoneNumber);
                if (!string.IsNullOrEmpty(contactItem.Email1Address))
                    dr[3] = contactItem.Email1Address;
                else
                    dr[3] = contactItem.Email2Address;
                dr[4] = Convert.ToString(contactItem.HomeAddress);
                dr[5] = Convert.ToString(contactItem.BusinessTelephoneNumber);
                dr[6] = Convert.ToString(contactItem.HomeTelephoneNumber);
                dr[7] = Convert.ToString(contactItem.CompanyName);
                dr[8] = Convert.ToString(contactItem.Birthday);
                dr[9] = Convert.ToString(contactItem.Anniversary);
                dr[10] = Convert.ToString(contactItem.JobTitle);
                dr[11] = Convert.ToString(contactItem.HomeFaxNumber);
                dt.Rows.Add(dr);
            }
        }
        ds.Tables.Add(dt);
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex);
    }
    return ds;
}

I added dll for this ,is of Microsoft.Office.Interop.Outlook with version 9.2 Description is Microsoft.Office 11.0 Object Library

It imports contact from outlook 2007 version but
when i'm importing contact from outlook 2010 then it is not importing contact.

How to resolve this problem?


Solution

  • You are working with version specific MS Office interop components.

    What you need to use is a non-version specific MS Office framework/library such as:

    NetOffice - The easiest way to use Office in .NET http://netoffice.codeplex.com/

    .NET Wrapper Assemblies for accessing Microsoft Office, Excel, Word, Outlook, PowerPoint, Access, Project

    With features such as:

    • Office integration without version limitations
    • All objects, methods, properties and events of the Office versions 2000, 2002, 2003, 2007, 2010 are included
    • Syntactically and semantically identical to the Microsoft Interop Assemblies No deployment hurdles, no problematic registration, no dependencies, no interop assemblies, no need for VSTO

    There is an example of how to get a list of Outlook contacts in C# here: http://netoffice.codeplex.com/wikipage?title=Outlook_Example05