3
Answers

Getting contacts from outlook

Carl-Johan Larsson

Carl-Johan Larsson

8y
272
1

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)