Programmatically access Outlook contacts
This example finds all contacts whose last names contain a specified search string.
Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.
Example
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AccessContacts("Na");
}
private void AccessContacts(string findLastName)
{
Outlook.MAPIFolder folderContacts = this.Application.ActiveExplorer().Session.
GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items searchFolder = folderContacts.Items;
int counter = 0;
foreach (Outlook.ContactItem foundContact in searchFolder)
{
if (foundContact.LastName.Contains(findLastName))
{
foundContact.Display(false);
counter = counter + 1;
}
}
MessageBox.Show("You have " + counter +
" contacts with last names that contain "
+ findLastName + ".");
}
Compile the code
This example requires:
- Contacts whose last names contain the string "Na" (for example, Tzipi Butnaru) in the Contacts folder.
Related content
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for