Getting contacts from outlook

May 5 2016 6:47 AM

Hi,


Im trying to get all the contacts that I have in outlook and writing them to a textbox.


This is my code:

// Obtain an instance of the Outlook application
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

// Access the MAPI namespace
Outlook.NameSpace ns = app.GetNamespace("MAPI");

// Get the user's default contacts folder
Outlook.MAPIFolder contacts = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

// Go through all the folders
foreach (Outlook.Folder folder in contacts.Folders)
{

// Iterate through each contact
for (int i = 1; i < folder.Items.Count + 1; i++)
{

// Get a contact
Outlook.ContactItem contact = (Outlook.ContactItem)folder.Items[i];

// Write contact info to textbox
richTextBox1.Text += folder.AddressBookName.ToString() + " " + contact.EntryID + " " + Environment.NewLine;

}
}


But I only get the contacts in the first folder, where am I going wrong?


Answers (3)