Introduction
This article shows how to create a simple Microsoft Office Outlook Addin which adds a button and displays some information about the contacts of a specified folder.
This is my first CodeProject article. I hope it provides everything you need!
Background
This sample uses VSTO 2005 SE (Visual Studio Tools for Office 2005 SE) for creating the Addin.
You can get VSTO here.
Microsoft Office (at least Outlook) must be installed!
Using the Code
I assume you are able to create an Outlook addin with VSTO. If you are not, you should read related articles (MSDN provides a lot of information) or use the sample project provided with this article.
Please check out the sample source code for details of the implementation!
First we have to create a button for our addin:
Office.CommandBars cmdBars = Application.ActiveExplorer().CommandBars;
Office.CommandBar cmdBar = cmdBars["Standard"];
try
{
m_btnSampleAddin = (Office.CommandBarButton)cmdBar.Controls[SampleButtonTag];
}
catch (Exception)
{
m_btnSampleAddin = null;
}
if (m_btnSampleAddin == null)
{
m_btnSampleAddin = (Office.CommandBarButton)cmdBar.Controls.Add(
1, missing, missing, missing, missing);
m_btnSampleAddin.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
m_btnSampleAddin.Caption = SampleButtonTag;
m_btnSampleAddin.Tag = SampleButtonTag;
}
m_btnSampleAddin.Click +=
new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
(m_btnSampleAddin_Click);
Within the click event handler of our newly created button, we let the user select an Outlook Folder using the default folder selection dialog:
Outlook.MAPIFolder folder = Application.Session.PickFolder();
If the user has selected a folder and confirmed this action by clicking "OK" the value of "folder" will be != null. In this case we show the "contacts" dialog and pass the selected folder to the constructor.
if (folder != null)
{
ContactDisplay contactDisplay = new ContactDisplay(folder);
contactDisplay.Show();
}
Within the load method of the contacts form, we will iterate through all objects in the specified folder. If the object is a "contact" item, we will add it to our list control:
foreach (object objItem in m_folder.Items)
{
if (objItem is Outlook.ContactItem)
{
Outlook.ContactItem contact = objItem as Outlook.ContactItem;
ListViewItem lvi = new ListViewItem(new string[]{
contact.FirstName,
contact.LastName,
contact.FullName,
contact.Email1Address
});
lvi.Tag = contact;
lvContacts.Items.Add(lvi);
}
}
It's important to preserve the ContactItem
object in the ListViewItem
Tag so that we can access this item later!
If you double click a listview
item, we read the tag and cast it to an Outlook ContactItem
. Thereafter we will show the inspector of this item.
if (lvContacts.SelectedItems.Count > 0)
{
ListViewItem lvi = lvContacts.SelectedItems[0];
Outlook.ContactItem contact = lvi.Tag as Outlook.ContactItem;
contact.GetInspector.Display(Type.Missing);
}
The Inspector
is an object which is nearly available for every Outlook item and shows you the default Outlook view for that item.
Points of Interest
With the old component model of Office, you had to mess with some restrictions (for example you weren't able to read the email address of contacts without prior authorization of the user). Creating Office Addins with VSTO is also much easier to do!
History
- 31.05.2007: Article updated
- 30.05.2007: Initial release