-4

I want to get the contacts of outlook to C# form in grid view. I have used the Microsoft outlook 12.0 Object Library 9.3 version. I am trying to get the contacts displayed in gridview as the getcontact button is clicked.

Code is pasted below; kindly check it and help me out to solve the issue.

   private void button1_Click(object sender, EventArgs e)
        {
            GetContacts();
        }
                public DataSet GetContacts()
                {
                    DataSet ds = new DataSet();
                    ds.Tables.Add("Contacts");
                    ds.Tables[0].Columns.Add("Email");
                    ds.Tables[0].Columns.Add("FirstName");
                    ds.Tables[0].Columns.Add("LastName");

                Microsoft.Office.Interop.Outlook.Items OutlookItems;
                Microsoft.Office.Interop.Outlook.Application outlookObj;
                MAPIFolder Folder_Contacts;

                outlookObj = new Microsoft.Office.Interop.Outlook.Application();
                Folder_Contacts =
    (MAPIFolder)outlookObj.Session.GetDefaultFolder(
    OlDefaultFolders.olFolderContacts);
                OutlookItems = Folder_Contacts.Items;

                for (int i = 0; i < OutlookItems.Count; i++)
                {
                    Microsoft.Office.Interop.Outlook.ContactItem contact =
    (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];
                    DataRow dr = ds.Tables[0].NewRow();
                    dr[0] = contact.Email1Address;
                    dr[1] = contact.FirstName;
                    dr[2] = contact.LastName;

                    ds.Tables[0].Rows.Add(dr);
                    dataGridView1.DataSource = dr;
                    richTextBox1.Text = dr.ToString();
                }

                dataGridView1.Show();
                return ds;
            }
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
user2420211
  • 280
  • 1
  • 3
  • 11

1 Answers1

0

I have tried the below mentioned code to fetch the data from outlook to c# desktop Application in gridview... I used the above mentioned API for that and got the email address of outlook that is configured on your system! the code is pasted below. the used API works fine with outlook 2007 and 2003... but for outlook 2010... its suggested to use the other API!!

The code is pasted below.

private void button1_Click(object sender, EventArgs e)
    {

        fetchOutlookContacts();
    }

    public void fetchOutlookContacts()
    {


        Microsoft.Office.Interop.Outlook.Items OutlookItems;
        Microsoft.Office.Interop.Outlook.Application outlookObj;
        MAPIFolder Folder_Contacts;

        outlookObj = new Microsoft.Office.Interop.Outlook.Application();
        Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
        OutlookItems = Folder_Contacts.Items;

        DataTable dt = new DataTable();
        dt.Columns.Add("Email Address");

        for (int i = 0; i < OutlookItems.Count; i++)
        {
            Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];
            dt.Rows.Add(new object[] { contact.Email1Address });
            dataGridView1.DataSource = dt;
        }
        dataGridView1.Show();

    }
Irshad
  • 3,071
  • 5
  • 30
  • 51
user2420211
  • 280
  • 1
  • 3
  • 11