|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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
IntroductionThis 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! BackgroundThis sample is using 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 codeI 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: 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! //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 InterestWith 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! History31.05.2007 30.05.2007
|
|||||||||||||||||||||||||||||||||||||||||||||||||||