Simple Outlook 2007 VSTO 2005 SE add-in that shows ItemContextMenuDisplay and Table usage

Someone on the team was looking to use VSTO 2005 SE to display a context menu in Outlook 2007 when you right-click on a mail item to delete all related mail items.  I put together some quick and dirty code that shows how to do this.  The interesting things here I think is the new Outlook 2007 ItemContextMenuDisplay event and the use of the new Outlook 2007 table object.  You can read more about the table object here  Also, this add-in doesn’t actually delete anything yet, it just shows a dialog box with what it would have deleted.




using System;

using System.Windows.Forms;

using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Outlook = Microsoft.Office.Interop.Outlook;

using Office = Microsoft.Office.Core;


namespace OutlookAddIn1


    public partial class ThisAddIn


        private Office.CommandBarButton btn;

        private Outlook.MailItem mail;


        private void ThisAddIn_Startup(object sender, System.EventArgs e)


            this.Application.ItemContextMenuDisplay += new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);



        void Application_ItemContextMenuDisplay(Office.CommandBar CommandBar, Outlook.Selection Selection)


            if (Selection.Count > 0)


                mail = Selection[1] as Outlook.MailItem;

                if (mail != null)


                    btn = CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, missing) as Office.CommandBarButton;

                    btn.Caption = "Delete related...";

                    btn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(btn_Click);





        void btn_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)


            if (mail != null)


                string subject = mail.Subject;

                string filter = @"@SQL=""urn:schemas:httpmail:subject"" like '%" + subject + "%'";

                Outlook.Table tbl = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).GetTable(filter, Outlook.OlTableContents.olUserItems);

                string result = "";



                while (!tbl.EndOfTable)


                    Outlook.Row row = tbl.GetNextRow();

                    string EntryID = row["EntryID"].ToString();

                    Outlook.MailItem oMail = (Outlook.MailItem)Application.Session.GetItemFromID(EntryID, Type.Missing);

                    result += oMail.Subject + " from " + oMail.SenderName + " on " + oMail.SentOn.ToString() + System.Environment.NewLine;

                    // TODO: Actually delete it (oMail.Delete())


                MessageBox.Show("Would have deleted: " + System.Environment.NewLine + result);






        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)




        #region VSTO generated code


        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InternalStartup()


            this.Startup += new System.EventHandler(ThisAddIn_Startup);

            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);






Comments (3)
  1. Beau says:

    Can you do this in Outlook 2003?

  2. Cyril says:

    Hello Eric,

    I sure wish Word had an equivalent for the ItemContextMenuDisplay event.
    Right now, we must use WindowBeforeRightClick. There are two drawbacks to this approach:

    – It does not work if the user presses the context menu keyboard key (this is bad for acccessibility)
    – We must grab the appropriate CommandBar object, which depends on the selection (text, table …).

    Do you know if there is any short-time plan to add this kind of event to the Word OM ?

    Thanks a lot.

  3. WHong says:

    Sorry, this comment was meant for a post which has comment disabled.

    I found almost all article links are invalided in your previous posts. Could you please update the link?

    Thanks a lot

Comments are closed.

Skip to main content