Outlook C# Extensibility: Contacts Without a Photo
Gosh I love extensibility. I'd so much rather take the time to write an app to automate a 1min task, if I had to do that task over and over again. MS Office has great extensibility. Here's a quick example of an app to list out all your contacts that don't have a photo on them yet.
The point of this is to show how you can very easily write C# code to pull custom reports from the Outlook data store. I keep a photo collection of faces I crop from pictures and I like to have the photos on my contacts. This tiny app just helps by letting me know which contacts need a pic.
I've got a good dozen or so custom Outlook apps, including apps to: custom filter mail, remove those pesky Appointment Reminders, create contacts from text files, count the amount of mail sent, create e-mail lists, export tasks to an XML file, create RSS feeds for mail, repair "Display As" contact fields, etc.
There are many types of Office extensibility apps, writing COM Addins, VBA in your Office file, using VSTO to write .NET code to tightly integrate within an office file (very cool), but this is just a simple example of writing a standalone .NET app to get data from Outlook.
Sample Code: OutlookContactsWithoutPhotos.zip
To use this example (with Visual Studio & Microsoft Outlook):
- Create a new console application project
- Change the project type (in the project properties) to a "Windows Application"
- Add a project references to the COM library "Microsoft Outlook x Control Library"
- Put this code in your app & run.
using System;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookContactsWithoutPhotos
{
class Program
{
static void Main(string[] args)
{
// Obtain an instance of the Outlook application
Outlook.Application app = new Outlook.ApplicationClass();
// 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);
// Iterate through each contact
for (int i = 1; i < contacts.Items.Count + 1; i++)
{
// Get a contact
Outlook.ContactItem contact =
(Outlook.ContactItem)contacts.Items[i];
// If this contact has a first name and no photo, list it
if (contact.FirstName != null &&
contact.FirstName.Trim().Length > 0 &&
!contact.HasPicture) Trace.WriteLine(contact.FullName);
}
}
}
}
References
- MSDN Library: GetDefaultFolder Method
- MSDN Library: Microsoft Outlook Visual Basic Reference
- MSDN Library: Outlook 2007 API Updates
- MSDN Library: Tools and Technologies for Office
- Book: Microsoft Outlook Programming (Jumpstart for Administrators, Developers, and Power Users)
- Search: Outlook C#
Comments
Anonymous
February 22, 2007
Hi Thanks for this example. I tried to use it to retrieve all the fields from a contact, including custom fields. I used a foreach loop, setting the text in a variable. I realised that sometimes, the value field contained the value "�", which ended my string variable. I spent a couple of hours on the problem before finding this out.Anonymous
July 17, 2007
Very useful. I wish you would post a short article on our wiki. C# Online.NET Editor http://en.csharp-online.net/Anonymous
September 17, 2008
Hi, Its Very useful.. But have you any idea about Contacts With a Photo.. I want outlook photo to retrive in my database!! Regards Ctal