Microsoft Office Outlook Programming Using VSTO 3.0 and C#: Part 1

Exclusive offer: get 50% off this eBook here
VSTO 3.0 for Office 2007 Programming

VSTO 3.0 for Office 2007 Programming — Save 50%

Get to grips with Programming Office 2007 using Visual Studio Tools for Office

$20.99    $10.50
by Ayman Hourieh | March 2009 | Open Source Web Development

Microsoft Office Outlook is one of the world's most widely-used personal information management tool. Primarily, the Microsoft Office Outlook serves as an email application for a wide range of users. In this article by Vivek Thangaswamy, you will learn the concepts of programming for Microsoft Office Outlook 2007 using VSTO 3.0 and C#. We will take a look at the following:

  • An overview of the Outlook object model and its features in VSTO
  • Learning the extensibility of Microsoft Office Outlook 2007 using the object model
  • Learning to customize Microsoft Office Outlook menus and toolbars using VSTO
  • Working with form regions in Outlook, manipulating folders, contact information, and mail items using VSTO programming

Microsoft Office Outlook object model overview

VSTO 3.0 for Office 2007 Programming

Programming in Microsoft Office Outlook differs from programming other Microsoft applications such as InfoPath and Excel. Most of the Microsoft Office applications target documents rather than data items, but Microsoft Office Outlook targets each data item stored in the database or the primary data storage used by an Outlook application. Microsoft Office Outlook stores and manages data items such as emails, appointments, notes, tasks, and contacts in tables in a structured database.

The Microsoft Office Outlook object model is based on COM (Component Object Model) and is used to interact with Outlook form regions, menus, and other application customization possibilities. It is similar to other Microsoft Office application object models when thought of with respect to the object model implementation interfaces for collections, objects, properties, methods, and events.

COM is a language-neutral way of implementing objects that can be used in different environments.

A large numbers of objects are available for developing and customizing Outlook 2007. If the developer needs to develop application add-ins for Outlook, he or she can program using the objects provided by the Outlook object model. In the Outlook object model, the class will represent each object in the UI to the user. For example, the Microsoft.Office.Interop.Outlook.Application class represents the entire application, and the Microsoft.Office.Interop.Outlook.MailItem class represents an email message.

To get used to the object models in Outlook, you should be familiar with some of the top-level objects. The Application object represents an Outlook application, and it is the highest level class in the Outlook object model. The Explorer object corresponds to the window that displays the contents of a folder, and contains Outlook data items such as email messages, tasks, appointments, and so on. The MAPIFolder object represents the folder that contains emails, contacts, tasks, and other Outlook data items. By default, there are sixteen MAPIFolder objects available. The Inspector object corresponds to a window that displays a single item such as a particular email message, or a specific contact item.

Let's see how to create application-level add-ins for Microsoft Office Outlook 2007 using VSTO 3.0. We will create a Hello World application-level example for Microsoft Office Outlook 2007 using Visual Studio 2008.

  1. Open Visual Studio 2008 to create a new Outlook 2007 Add-in template project.
  2. Select New Project. Under Office select 2007, and then select the Microsoft Outlook Add-in template, and name the project as per your requirements.

    VSTO 3.0 for Office 2007 Programming

  3. The solution will be created with all of the supporting files required for the development of Outlook solution, as shown in the following screenshot:

    VSTO 3.0 for Office 2007 Programming

  4. Write the following code to display a message box:
    // Windows forms namespace to display Message box
    using System.Windows.Forms;
  5. Write the code to display the Say Hello World! message while loading:
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    // Message box display
    MessageBox.Show("Say Hello World!");
    }

The result will be similar to what is shown in the following screenshot:

VSTO 3.0 for Office 2007 Programming

Outlook 2007 object models are categorized and mapped correspondingly. The following Outlook object models hold all of Outlook's objects for programming in their corresponding object model categories: Items object model, Navigation bars and Outlook bars object model, Rules object model, and Views object model. In the Application object model hierarchy, the Application object is the parent of all other Outlook objects.

Customization using VSTO

Most enterprise business applications are loaded with a wide variety of features for users. Even though these applications have a wide range of features, many business requirements can be fulfilled only through customization because not all applications are designed to fit each and every enterprise's special needs. Customization for most of the application is a tough job to execute.

Outlook 2007 is loaded with a wide variety of features that will satisfy a broad range of user categories. Some organizations need more functionality and features to be added, so that the application will satisfy their custom business requirements. VSTO 3.0 helps Office developers to customize and enhance the Outlook 2007 application as per the user's business requirements.

Microsoft Office Outlook 2007 supports other Microsoft Office tools such as InfoPath and Excel, in order to provide seamless collaboration.

VSTO provides an easy way to create an application-level add-in for Outlook 2007 using Visual Studio 2008. Creating add-ins for Microsoft Office Outlook 2007 has been more complex to work out, but VSTO 3.0 offers Office developers with project templates in Visual Studio 2008 to allow them to create add-ins for Outlook 2007. Adding to the project templates, VSTO offers great support for development and deployment, which improves the development work. This provides .NET framework support for Outlook 2007 add-in programming, which includes class library support, controlled exception handling, memory management, extensibility, ClickOnce deployment, and so on.

Menus in Outlook

A group of commands or lists of options from which you can choose your desired operation is known as a menu. Most of the latest applications are menu driven. Microsoft Office Outlook, which is a menu-driven application, provides the user with an easy fl owing UI for user interaction. Most of the menu-driven applications will provide you with basic customization such as choosing the menu for the default view of the application, and so on.

In Microsoft Office, all menus and toolbars are CommandBars. A CommandBar is a static collection shared by all Windows. There are standard toolbars, menu bars, context menus, and so on. A small add-in enumerates and displays all Microsoft Outlook CommandBars.

Likewise, Microsoft Office Outlook 2007 provides the option for a user to customize the menus. VSTO 3.0 provides Office developers with the ability to build custom menus and customize the existing menus using the .NET framework and support a programming language. You can even rebuild the classic menu style for Outlook 2007 by using the VSTO 3.0 application level add-ins development.

Let's create a custom menu in the menu bar of the Outlook and add a new item to the menu. This way, you will get to know about custom menu development for Microsoft Office Outlook 2007.

Open Visual Studio 2008 and create a new solution, as described in the previous example.

Let's write a program to create a menu item and call it Say Hello World.

// Defining new Menubar
private Office.CommandBar PacktOldMenuBar;
// Defining old Menubar
private Office.CommandBarPopup PacktNewMenuBar;
// Defining instance of button for menu item
private Office.CommandBarButton PacktButton1;
// Tag string for our Menu item
private string strMenuString = "Outlook AddIn #1";
private void ThisAddIn_Startup(object sender,
System.EventArgs e)
{
// Define the Old Menu Bar
PacktOldMenuBar = this.Application.ActiveExplorer().
CommandBars.ActiveMenuBar;
// Define the new Menu Bar into the existing menu bar
PacktNewMenuBar = (Office.CommandBarPopup)PacktOldMenuBar.
Controls.Add(Office.MsoControlType.msoControlPopup,
missing, missing, missing, false);
//If PacktNewMenuBar not found then the code will add it
if (PacktNewMenuBar != null)
{
// Set caption for the Menu
PacktNewMenuBar.Caption = "Packt Menu Item 1";
// Tag string value passing
PacktNewMenuBar.Tag = strMenuString;
// Assigning button type
PacktButton1 = (Office.
CommandBarButton)PacktNewMenuBar.
Controls.Add(Office.MsoControlType.
msoControlButton, missing, missing, 1, true);
// Setting up the button style
PacktButton1.Style = Office.MsoButtonStyle.
msoButtonIconAndCaptionBelow;
// Set button caption
PacktButton1.Caption = "Say Hello World";
// Set the menu visible
PacktNewMenuBar.Visible = true;
}
}

The following screenshot displays the resulting menu developed by you using the preceding code:

VSTO 3.0 for Office 2007 Programming

You can also build more custom menus for your Outlook 2007 with functionality as per your requirements. VSTO will speed up development and provide support for a hassle free environment for the developer to work on Outlook 2007 add-ins and other customization.

Toolbars in Outlook

Generally, toolbars provide easy access to the functionality of the application by using buttons and menus. Most application's user interfaces have a toolbar that has buttons, menus, and input or output control elements for user interaction with the application. Even applications allow users to do visual customization of toolbars as per the users' needs.

Microsoft Office Outlook 2007 provides a very good visual representation of toolbars for user interaction with the application. Outlook provides support for toolbar customization to improve custom visual interaction for users. VSTO 3.0 offers wide options to build custom toolbars and to customize existing toolbars as per the user's needs.

Remember that menus and CommandBars are not VSTO features, but are in the Microsoft.Office namespace. VSTO is making it easier to program for the Office object model.

Let's see a demonstration of creating a custom toolbar with a button.

  1. Open Visual Studio 2008, and create a new solution, as described above.
  2. Next, add the reference needed to show the message box needed in our demonstration:
    // Namespace reference for message box
    using System.Windows.Forms;
  3. Let's write a program to create a toolbar and call it Hello World!.
    Office.CommandBar PacktCustomToolBar;
    // Declare the button
    Office.CommandBarButton PacktButtonA;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    // Verify the PacktCustomToolBar exist and add to the application
    if (PacktCustomToolBar == null)
    {
    // Adding the commandbar to Active explorer
    Office.CommandBars PacktBars = this.Application.
    ActiveExplorer().CommandBars;
    // Adding PacktCustomToolBar to the commandbars
    PacktCustomToolBar = PacktBars.Add("NewPacktToolBar",
    Office.MsoBarPosition.msoBarTop, false, true);
    }
    // Adding button to the custom tool bar
    Office.CommandBarButton MyButton1 = (Office.
    CommandBarButton)PacktCustomToolBar.Controls.Add(1,
    missing, missing, missing, missing);
    // Set the button style
    MyButton1.Style = Office.MsoButtonStyle.msoButtonCaption;
    // Set the caption and tag string
    MyButton1.Caption = "PACKT BUTTON";
    MyButton1.Tag = "MY BUTTON";
    if (this.PacktButtonA == null)
    {
    // Adding the event handler for the button in the toolbar
    this.PacktButtonA = MyButton1;
    PacktButtonA.Click += new Office.
    _CommandBarButtonEvents_ClickEventHandler(ButtonClick);
    }
    }
    // Button event in the custom toolbar
    private void ButtonClick(Office.CommandBarButton ButtonContrl,
    ref bool CancelOption)
    {
    // Message box displayed on button click
    MessageBox.Show(ButtonContrl.Caption + " Says Hello World!");
    }

The following image shows the results of adding a custom toolbar with button control:

VSTO 3.0 for Office 2007 Programming

The CommandBars object helps you to build variants of toolbars to your Outlook 2007 application's user interface. Microsoft Office Outlook 2007 is the only tool in the Microsoft Office 2007 family to support both standard toolbars and the new Ribbon in their UI.

VSTO 3.0 for Office 2007 Programming Get to grips with Programming Office 2007 using Visual Studio Tools for Office
Published: March 2009
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

Outlook form regions support

Before the release of Microsoft Office Outlook 2007, Outlook forms were the only option for creating a custom UI. In Outlook 2007, you have a new feature called form regions. Form regions are a new way of customizing the Outlook. Form regions are the added feature of the UI in the standard Microsoft Office Outlook 2007. Form regions will add custom functionality–they will build a new range of options in the UI for Microsoft Office Outlook 2007.

When you work with standard forms, you can see that all Outlook items have a MessageClass property that determines the type of objects you're working on, for example, contact, appointment, task, email, post, or journal entry. All of the standard forms are stored in the standard forms library. Form regions are the replacement for the standard forms, to display the custom reading pane, and add new pages to Outlook. In form regions, the controls can be bound to the Outlook properties.

Outlook forms bring you a new design with the support of .NET controls, in order to easily integrate with your Visual Studio Designer and provide code templates and debugging options.

  1. Open Visual Studio 2008, to create a new Outlook 2007 Add-in template project.
  2. Select New Project. Under Office select 2007 and select Outlook 2007 Add-in template and name the project as per your requirement.
  3. The solution will be created with all of supporting files required for the development of our Outlook solution.
  4. Right-click on your project solution and click on the Add New Item option. Select the Outlook Form Region template as shown in the following screenshot:

    VSTO 3.0 for Office 2007 Programming

  5. After clicking on the Add button in the Visual Studio 2008 Add New Item dialog box, a wizard window in which you can set the properties for New Outlook Form Region will appear as shown in the following screenshot:

    VSTO 3.0 for Office 2007 Programming

  6. A form region can be created as either a separate form or as a form adjoining the existing form, or you can also overwrite an existing form with the new form you're creating. In the New Outlook Form Region wizard, you have options to select accordingly.

    VSTO 3.0 for Office 2007 Programming

  7. Name the new Outlook form region as per your requirements:

    VSTO 3.0 for Office 2007 Programming

  8. Next, associate the standard message classes to the Outlook form region:

    VSTO 3.0 for Office 2007 Programming

  9. Once you are done creating the Outlook form region, run the solution. The new form region will appear as shown in the following screenshot:

    VSTO 3.0 for Office 2007 Programming

  10. We can also write a program to display the current contact item's email address in the form region. Open the FormRegion1.cs file and write the following code inside the FormRegionShowing event:
    // Occurs before the form region is displayed
    // Use this.OutlookItem to get a reference to the current
    Outlook item
    // Use this.OutlookFormRegion to get a reference to the
    form region
    private void FormRegion1_FormRegionShowing(object sender,
    System.EventArgs e)
    {
    // Verify the current Outlook item is Outlook contact item
    if (this.OutlookItem is Outlook.ContactItem)
    {
    // Get reference through the Outlook contact item
    Outlook.ContactItem PacktMailContactItem = (Outlook.
    ContactItem)this.OutlookItem;
    // Message box displayed while you click the form region
    button in the Ribbon
    MessageBox.Show("This form region to display: " +
    PacktMailContactItem.Email1DisplayName);
    // The listview control displays the current contacts
    display email
    ContactListView.Items.Add(PacktMailContactItem.
    Email1DisplayName.ToString());
    }
    }

The addition and execution of the preceding code results in the following screenshot:

VSTO 3.0 for Office 2007 Programming

One of the key advantages of form regions is that you can use themed controls that match the look of standard Outlook forms in Outlook 2007, and you also have easy access to new controls for date and time picking, and time zones. Form regions will help you to build custom contact forms, meeting invitees, and enhanced UI for Microsoft Office Outlook 2007. Outlook 2007 also contains a huge number of new form controls that offer the business logic and performance that users expect from Outlook forms.

Manipulation

Manipulation is an essential element for the efficient development of a reliable solution to satisfy any kind of business need. Manipulation plays the most important role in customization and development of any kind of solution for mature enterprise applications. The primary part of manipulation is the data. Microsoft Office Outlook 2007 allows a wide range of data item manipulation methods inside the application—you can manipulate mail items, contact items, folders in Outlook, and so on. Let's see some examples of manipulation.

Folders

Folders are the containers that hold file items and sub folders inside the main folder. Folders are used to manage mail items to make them easily accessible inside Outlook. In Outlook 2007, you can manage mail items, contact items, and other communication information.
Let's start working with the Outlook folder using VSTO 3.0 and C#.NET programming. The following code will create a folder with your desired name, by using VSTO object models.

  1. Open Visual Studio 2008 to create a new Outlook 2007 Add-in template project.
  2. Select New Project. Under Office select 2007, and then select Outlook 2007 Add-in template and name the project as per your requirement.
  3. The solution will be created with all of the supporting files required for the development of our Outlook solution.
  4. Enter the following code, which will dynamically create a folder, inside the ThisAddIn.cs file:
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    // Get the namespace for Outlook operation
    Outlook.NameSpace ReadOutLookNameSpace = this.Application.
    GetNamespace("MAPI");
    // Get the default folder names from the Outlook
    Outlook.MAPIFolder ReadDefaultFolders = ReadOutLookNameSpace.
    GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    // Create folder in the "FolderPacktPub"
    ReadDefaultFolders = this.Application.Session.Folders[1].
    Folders.Add("FolderPacktPub", missing);
    }

We can see the dynamically-created folder named FolderPacktPub on the leftmost side of the Outlook screen, as shown in the following screenshot:

VSTO 3.0 for Office 2007 Programming

The folders contain Items collections which hold items that are compatible with the type of data that is supported in Microsoft Office Outlook 2007. The GetDefaultFolder() method is used to retrieve a reference to the Sent Items folder. This is declared in order to get default folders by using new Folder objects for Outlook 2007.

You may need to clean up Outlook by deleting unwanted folders. The delete operation for folders inside Outlook can be performed by using the MAPIFolder object and the properties and methods exposed by the object. Let's see an example of how to delete a folder using the VSTO object. In this example, you are going to run the operation at the initialization of the add-in. You can do the same thing by using the Click event for the button that was placed on the toolbar (or anywhere else you need).

Open Visual Studio 2008 and create a new solution, as explained earlier. Write the following code, which will dynamically delete a folder in the ThisAddIn.cs file:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Loading the NameSpace needed to manipulate MAPI
Outlook.NameSpace PacktNameSpace = this.Application.
GetNamespace("MAPI");
// Accessing the folder through MAPIFolder Object
// PacktNameSpace.Folders is a collection of all the folders
// We want to delete the 13th folder, FolderPackt
Outlook.MAPIFolder PacktFolders = PacktNameSpace.Folders[13];
// Getting the folder named FolderPackt
PacktFolders.Name = "FolderPackt";
// Doing the delete operation using Delete method
PacktFolders.Delete();
}
VSTO 3.0 for Office 2007 Programming Get to grips with Programming Office 2007 using Visual Studio Tools for Office
Published: March 2009
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

Contacts

Contacts contain information about communication with a person. The information can be their address, name, phone number, and other relevant information. The Contacts folder in Microsoft Office Outlook 2007 is the electronic form of managing contact information about people. Outlook offers the ability to very easily manage multiple phone numbers and more information for a single person. Outlook provides better management of contact entries and brings easy access through the information available in the Contacts folder.

In certain situations, you may need to add or modify the contact entries dynamically. To support these features, Microsoft provides a solution through VSTO 3.0 programming to allow Office developers to build application-level add-ins, which will help you integrate custom developed solutions.

We will now create a contact item using the VSTO 3.0 object model and C#. Open Visual Studio 2008 and create a new solution, as explained earlier. Write the following code, which will dynamically create a contact item in the ThisAddIn.cs file.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Using the Outlook object reading through the contact item
folders
// Preparing to create a new contact item
Outlook.ContactItem OutlookPacktContact = (Outlook.
ContactItem)this.Application.CreateItem(Outlook.
OlItemType.olContactItem);
// Set FirstName property
OutlookPacktContact.FirstName = "Radhika";
// Set LastName property
OutlookPacktContact.LastName = "Rajagopalan";
// Set Email1Address property
OutlookPacktContact.Email1Address = "radhika@vsto.com";
// Set CompanyName property
OutlookPacktContact.CompanyName = "ACC Inc";
// Set Categories property
OutlookPacktContact.Categories = "Packt Group";
// Now save the above contact item
OutlookPacktContact.Save();
}

After executing the source code, you will get the following screenshot as the output for your solution.

VSTO 3.0 for Office 2007 Programming

ContactItem is the object used to access the contact in a Contacts folder by using Outlook programming. The ContactItem object has been exposed with a wide variety of properties, events, and methods to program for the Contact folder in Outlook 2007.

You have seen how to create a new contact item by using an object in Outlook 2007. Let's take a look at one of the most important contact management features in Microsoft Office Outlook: the distribution list. A distribution list is a collection of email contact information that has been grouped into a single list. VSTO helps Office developers create and modify distribution lists by using the DistListItem object.

Let's create a distribution list dynamically, using the VSTO Outlook object model and C# programming.

Open Visual Studio 2008 and create a new solution, as described earlier. Write the following code, which will dynamically create a distribution list, inside the ThisAddIn.cs file:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Using the Outlook object reading through the Distribution item
// Preparing to create a new Distribution item
Outlook.DistListItem OutlookPacktDistributionList = (Outlook.
DistListItem)this.Application.CreateItem(Outlook.OlItemType.
olDistributionListItem);
// Name the new distribution list
OutlookPacktDistributionList.DLName = "PacktContactList";
// To set the recipient information, create the mailitem
collect object
Outlook.MailItem PacktListItem = (Outlook.MailItem)this.
Application.CreateItem(Outlook.OlItemType.olMailItem);
// Adding mail items
PacktListItem.Recipients.Add("Radhika Rajagopalan
<radhika@bookvsto.com>");
PacktListItem.Recipients.Add("Vivek Thangaswamy
<vivek@bookvsto.com>");
// Adding the recipients to the new distribution
OutlookPacktDistributionList.AddMembers(PacktListItem.Recipients);
// Save the new distribution using Save method
OutlookPacktDistributionList.Save();
}

The newly-distribution list appears as shown in the following screenshot:

VSTO 3.0 for Office 2007 Programming

On clicking the Members tab in the solution, we get a list of the recipients, or members, as shown in the following screenshot:

VSTO 3.0 for Office 2007 Programming

Email messages

Email is an electronic form of storing information that can be shared with people via an electronic communication system. You can write, send, and receive information without using paper, and communicate through electronic systems by using a digital copy of the information.

You can do many email operations through VSTO programming, such as composing mail items at runtime using Office objects. Let's create a new mail that has a Subject, To, Address and few other properties, by using the VSTO Outlook MailItem object.

Open Visual Studio 2008 and create a new solution, as explained earlier. Write the following code, which will dynamically create a mail item, inside the ThisAddIn.cs file:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Outlook mailitem object to compose new mail
Outlook.MailItem PacktMailItem = (Outlook.MailItem)this.
Application.CreateItem(Outlook.OlItemType.olMailItem);
// Set the To address property value
PacktMailItem.To = "radhika@vsto.com";
// Set the Subject property value
PacktMailItem.Subject = "Mail from PacktPub Editor";
// Set the Body property value
PacktMailItem.Body = "Your book is ready to buy!";
// Set the Importance level for the mail
PacktMailItem.Importance = Outlook.OlImportance.
olImportanceNormal;
// If parameter is set to false compose mail won't display
PacktMailItem.Display(true);
// To send the composed mailitem
// ((Outlook._MailItem) PacktMailItem).Send();
}

The results of the execution of the preceding code example can be seen in the following screenshot:

VSTO 3.0 for Office 2007 Programming

The Microsoft.Office.Interop.Outlook.MailItem class represents an email message. MailItem objects are usually found in folders, such as Inbox, Sent Items, and Outbox. The MailItem exposes properties and methods that can be used to create and send email messages.

With Outlook 2007 mail items, you have the option to categorize mail using colors. This categorization can be achieved by the use of VSTO objects. Mail items can be categorized programmatically. Outlook.MailItem is the object used to access mail items in Outlook 2007.

The following image is a typical view of the category option available in Outlook 2007:

VSTO 3.0 for Office 2007 Programming

Let's see in an example of how to get the category property by using the VSTO objects for the mail items.

Open Visual Studio 2008 and create a new solution, as described earlier. Write the following code to the context menu item in the mail box folder, inside the ThisAddIn.cs file:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Creates a new mail item instance
Outlook.MailItem OLmailitems = (Outlook.MailItem)this.Application.
CreateItem(OlItemType.olMailItem);
// Get the category dialog window for the mail items
OLmailitems.ShowCategoriesDialog();
}

After executing this program, we can see the different color properties, as shown in the following screenshot:

VSTO 3.0 for Office 2007 Programming

VSTO also supports the customization of Outlook 2007 context menu items. Let's look at an example to add a custom menu item in the context menu item of the email folder in Outlook.

Open Visual Studio 2008 and create a new solution, as explained earlier. Write the following code to the context menu item in the mail box folder, inside the ThisAddIn.cs file:

privat e void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Custom context menu item event managed
this.Application.ItemContextMenuDisplay += new Microsoft.Office.
Interop.Outlook.ApplicationEvents_11_
ItemContextMenuDisplayEventHandler(
PacktMenuItem_ItemContextMenuDisplay);
}
// Context menu item adding procedure
public void PacktMenuItem_ItemContextMenuDisplay(Microsoft.Office.
Core.CommandBar PacktCommandBar, Microsoft.Office.Interop.Outlook.
Selection Selection)
{
// Commadbarpopup control to context menu item
Office.CommandBarPopup PacktCustomItem =
(Office.CommandBarPopup)PacktCommandBar.Controls.Add(Office.
MsoControlType.msoControlPopup, Type.Missing,
"Custom Menu Item", PacktCommandBar.Controls.Count + 1,
Type.Missing);
// Added to separate group in context menu
PacktCustomItem.BeginGroup = true;
// Set the tag value for the menu
PacktCustomItem.Tag = "PacktCustomMenuItem";
// Caption for the context menu item
PacktCustomItem.Caption = "Custom Menu Item1";
// Set it to visible
PacktCustomItem.Visible = true;
}

The resulting custom menu item in the Inbox mail folder is visible in the following screenshot:

VSTO 3.0 for Office 2007 Programming

Summary

This article examined some important topics regarding programming using VSTO and C# for Microsoft Office Outlook 2007. You have seen the Outlook object model, and how it is used for Outlook application development and customization. This article covered the concepts of customizing the Outlook toolbar and menu bar using VSTO. You have learned about VSTO programming support for Outlook form regions. We have seen the most important part of Outlook data manipulation such as email, contacts, and folder manipulation. We have also seen code examples for each topic, which makes it easier to understand the concepts. The second part of this article will cover

Working with Appointments and meetings and also explain with an example, how to create a Ribbon menu for Outlook 2007.

About the Author :


Ayman Hourieh

Ayman Hourieh holds a bachelor degree in Computer Science. He joined the engineering team at Google in January 2008. Prior to that, he worked with web application development for more than two years. In addition, he has been contributing to several Open Source projects such as Mozilla Firefox. Ayman also worked as a teaching assistant in Computer Science courses for one year. Even after working with a variety of technologies, Python remains Ayman's favorite programming language. He found Django to be a powerful and flexible Python framework that helps developers to produce high-quality web applications in a short time.

Books From Packt


Joomla! 1.5: Beginner's Guide
Joomla! 1.5: Beginner's Guide

Refactoring with Microsoft Visual Studio 2010
Refactoring with Microsoft Visual Studio 2010

Microsoft Silverlight 4 Data and Services Cookbook
Microsoft Silverlight 4 Data and Services Cookbook

Microsoft Silverlight 4 Business Application Development: Beginner’s Guide
Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

Programming Microsoft Dynamics NAV 2009
Programming Microsoft Dynamics NAV 2009

Microsoft Dynamics AX 2009 Programming: Getting Started
Microsoft Dynamics AX 2009 Programming: Getting Started

Oracle JRockit: The Definitive Guide
Oracle JRockit: The Definitive Guide

Microsoft Dynamics AX 2009 Development Cookbook
Microsoft Dynamics AX 2009 Development Cookbook


Your rating: None Average: 4.5 (8 votes)
Very nice.... by
Very nice....
Click Once .... by
I don't know if anything in the above article is good because an Outlook 2007 Add-In created using the wizard generated code in Visual Studio can't be compiled. Without this error: error MSB3482 An error occurred while signing Invalid provider type specified
how to remove? by
yeah nice, but how to remove add-in would finished the article off. Therefore, I give it a 1 star.
How to remove the Add-in? by
Hello, thanks for the great article but how please to remove the Add-in that is installed during build process even not installed? I mean, I have used you code to add an item to the mail item context menu, when build it added the Item but how to remove it since it is not in Add/Remove programs in control panel? Also can you please provide to add the "Custom menu item 1" example without having the arrow next to it? I mean a single menu without SubItems, Thanks a lot.
How to remove the addin? by
Hello, Thanks for the great article, but how to remove the added addin? I just tried the exapmle of "Packt Menu Item 1" and i have now about 5 menus "Packt Menu Item 1" , so how can I remove them pls?
Very nice article by
Good Job
Task and measurement of response time by
I want to use outlook this way: I want to create a new form, based on task request form. then I want that every time the person who receive this task, and complete it, and mark it like that produce a mesage in order to measure which was tis response time, and then feed a database to make record in order to make management of the same. It look possible? what do you think?
Thanks by
Very good article. Helped me a great deal in my project.
Thanks for the article, it's by
Thanks for the article, it's very helpful for beginners.

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
T
j
T
P
M
k
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software