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 http://msdn2.microsoft.com/en-us/library/ms772423.aspx.  Also, this add-in doesn’t actually delete anything yet, it just shows a dialog box with what it would have deleted.

 

E.

 

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);

        }

 

        #endregion

    }

}