Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Article

Introduction to Interop with Outlook, VSTO and C#

Rate me:
Please Sign up or sign in to vote.
3.57/5 (10 votes)
29 May 2007CPOL2 min read 65.1K   1K   38   2
This article shows you how to create an Outlook addin with VSTO and read some information about contacts
Screenshot - outlook1.png

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:

C#
//First we try to get the default toolbar
Office.CommandBars cmdBars = Application.ActiveExplorer().CommandBars;

//In the German version of Outlook the default command bar
//is named "Standard". In other language versions of Outlook
//this name could differ, then you need to modify the next line
Office.CommandBar cmdBar = cmdBars["Standard"];

//Try to get the existing Button
try
{
    m_btnSampleAddin = (Office.CommandBarButton)cmdBar.Controls[SampleButtonTag];
}
catch (Exception)
{
    m_btnSampleAddin = null;
}

//If the button doesn't exist, create a new one
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;
}

//Add a click handler for this button
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:

Screenshot - outlook4.png

C#
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.

C#
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:

C#
foreach (object objItem in m_folder.Items)
{
    //if the item is an Outlook contact, display its information in the listview
    if (objItem is Outlook.ContactItem)
    {
        Outlook.ContactItem contact = objItem as Outlook.ContactItem;
        //Fill the list view item with some information about the contact
        ListViewItem lvi = new ListViewItem(new string[]{
            contact.FirstName,
            contact.LastName,
            contact.FullName,
            contact.Email1Address
        });

        //and finally set the contact as the list view item's tag so we
        //later can access this contact object
        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!

Screenshot - outlook5.png

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.

C#
//If a list view item has been selected
if (lvContacts.SelectedItems.Count > 0)
{
    ListViewItem lvi = lvContacts.SelectedItems[0];

    //get the contact information from the item tag
    Outlook.ContactItem contact = lvi.Tag as Outlook.ContactItem;

    //and display the outlook inspector of the contact
    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRemove button Pin
Nguyen Phuoc Dai22-Aug-07 15:45
Nguyen Phuoc Dai22-Aug-07 15:45 
AnswerRe: Remove button Pin
Christoph Schick23-Aug-07 8:26
Christoph Schick23-Aug-07 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.