5,427,303 members and growing! (20,667 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Introduction to Interop with Outlook, VSTO and C#

By Christoph Schick

This article shows you how to create an Outlook addin with VSTO and read some informations about contacts
C# 2.0, C#, Windows, .NET, .NET 2.0, COM, VS2005, Visual Studio, Dev

Posted: 29 May 2007
Updated: 29 May 2007
Views: 13,883
Bookmarked: 15 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.41 Rating: 3.10 out of 5
1 vote, 16.7%
1
2 votes, 33.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
3 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
Screenshot - outlook1.png

Introduction

This article shows how to create an simple Microsoft Office Outlook Addin which adds a button and display some informations about the contacts of a specified folder.

This is my first Codeproject article, I hope it provides everything you need!

Background

This sample is using VSTO 2005 SE (Visual Studio Tools for Office 2005 SE) for creating the Addin.

You can get VSTO here:
http://www.microsoft.com/downloads/details.aspx?displaylang=de&FamilyID=5E86CAB3-6FD6-4955-B979-E1676DB6B3CB

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 implemenation!

First we have to create a button for our addin:

//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, than 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 an 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

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 trough all objects in the specified folder. If the object is an "contact" item we will add it to our list control:

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 informations 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.

//If an 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 wasn'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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Christoph Schick


Christoph Schick was born 1984 in Graz, Austria.


At the age of 11 he was coding his first very small applications on an old second hand C128, the prefered language was Basic, what else Wink


Since then he was fascinated of programming, and 6 years later he begun to educate his passion Big Grin



Now he is working as developer for 4 years in an IT company located in Graz.


Christoph Schick's Homepage - http://www.chrisch.info
Occupation: Web Developer
Location: Austria Austria

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralRemove buttonmemberNguyen Phuoc Dai16:45 22 Aug '07  
AnswerRe: Remove buttonmembercschickat9:26 23 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 May 2007
Editor:
Copyright 2007 by Christoph Schick
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project