1

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?

Yuck
  • 49,664
  • 13
  • 105
  • 135
Priyanka
  • 2,802
  • 14
  • 55
  • 88
  • @Daniel HilgarthIt : is not showing any error, it only displays my defined error message. That is contact not available. – Priyanka Sep 07 '11 at 12:21

1 Answers1

3

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

Carlos Quintanilla
  • 12,937
  • 3
  • 22
  • 25
  • As u told i have to use the NetOffice dll but which is also one of the dependency . It means i have to add that NetOffice dll externally in my application. But is there any other solution on this problem? – Priyanka Sep 07 '11 at 12:20
  • If you want to do it yourself (not using NetOffice), then you need to add code to verify which office version is installed and then reference to the correct Interop components Microsoft.Office 11.0 Object Library or the one for Office 2010. – Carlos Quintanilla Sep 07 '11 at 12:27
  • but how to do this at runtime . Because i'm developing one software which is having feature of outlook contact importing. And this software will be used by different user & how should i recognize at runtime that which outlook version is used & how can i add the required Interop components at runtime? – Priyanka Sep 07 '11 at 12:48
  • 1
    There might be different ways to do that. I found this one here in Stackoverflow: http://stackoverflow.com/questions/3266675/how-to-detect-installed-version-of-ms-office – Carlos Quintanilla Sep 07 '11 at 12:56
  • Once you identify which version of Office the client has installed, then you need to load your assembly that references the correct MS Office version of the Interop Components. Probably using Reflection to load on runtime your assembly. – Carlos Quintanilla Sep 07 '11 at 12:58