Accessing Outlook contacts list in C#

This article show how to access Outlook Contact list

For better understanding we take an example. In this example there is one window form with datagridview. We have to show all contacts in this datagridview, something like this:

Step1.JPG

Start working with  Microsoft.Office.Interop.Outlook:

Add referance  of Microsoft.Office.Interop.Outlook library(example is using Microsoft.Office.Interop.Outlook 12.0)

Add one DatagridView and one button on form1.

And declare:

Microsoft.Office.Interop.Outlook.Items OutlookItems;

Microsoft.Office.Interop.Outlook.Application outlookObj;

MAPIFolder Folder_Contacts;

 

Work on Form1:


private void Form1_Load(object sender, EventArgs e)

{

    Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();

    MAPIFolder Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts); 

    OutlookItems = Folder_Contacts.Items; 

    for (int i = 0; i < oItems.Count; i++)

    {

        Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];

        dataGridView1.Rows.Add();

        if (contact.FirstName != null)

        {

            dataGridView1.Rows[i].Cells[0].Value = contact.FirstName;

        }

        else

        {

            dataGridView1.Rows[i].Cells[0].Value = "";

        }

        if (contact.MiddleName != null)

        {

            dataGridView1.Rows[i].Cells[1].Value = contact.MiddleName;

        }

        else

        {

            dataGridView1.Rows[i].Cells[1].Value = "";

        }

        if (contact.LastName != null)

        {

            dataGridView1.Rows[i].Cells[2].Value = contact.LastName;

        }

        else

        {

            dataGridView1.Rows[i].Cells[2].Value = "";

        }

        if (contact.Email1Address != null)

        {

            dataGridView1.Rows[i].Cells[3].Value = contact.Email1Address;

        }

        else

        {

            dataGridView1.Rows[i].Cells[3].Value = "";

        }

        }

}


Add code for showing contact'description on new form frmEditContact :

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

{

    if (e.RowIndex != -1)

    {

        Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[e.RowIndex + 1];

        frmEditContact frm = new frmEditContact();

        frm.Fname = contact.FirstName;

        frm.Mname = contact.MiddleName;

        frm.Lname = contact.LastName;

        frm.Eamil = contact.Email1Address;

        frm.ShowDialog();

    }

}

Work on frmEditContact:

public partial class frmEditContact : Form

{

    public frmEditContact()

    {

        InitializeComponent();

    }

    public string Fname = "";

    public string Mname = "";

    public string Lname = "";

    public string Eamil = "";

    private void frmEditContact_Load(object sender, EventArgs e)

    {

        txtFname.Text = Fname;

        txtMname.Text = Mname;

        txtLname.Text = Lname;

        txtEmail.Text = Eamil;

    }

}

frmEditContact will show as:

Step2.JPG


Similar Articles