If you use a contextmenu in you’re Microsoft Outlook 2010 C# add-in and want to use the item the user clicked on, you should use the first object in the

Globals.ThisAddIn.Application.ActiveExplorer().Selection

image

 

Note

The Selection collection does not start at index 0 but index 1, if you use Selection[0] an index out of bounds exception will occur.

 

ContextMenu.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
    <contextMenus>
        <contextMenu idMso="ContextMenuFolder">
            <button idMso="FolderPropertiesContext" getVisible="IsVisible" />
            
        </contextMenu>
        <contextMenu idMso="ContextMenuMailItem">
            <button id="MyContextMenuMailItem"
            label="Start Timer"
            onAction="OnMyButtonClick"/>
        </contextMenu>
        <contextMenu idMso="ContextMenuTaskItem">
            <button id="MyContextMenuTaskItem"
                    label="Start Timer"
                    onAction="OnMyButtonClick"/>
        </contextMenu>
        <contextMenu idMso="ContextMenuCalendarItem">
            <button id="MyContextMenuCalendarItem"
          label="Start Timer"
          onAction="OnMyButtonClick"/>
        </contextMenu>
    </contextMenus>
</customUI>

 

ContextMenu.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Ada.Tip.WpfUserControls.BC;

namespace Ada.Tip.OutlookAddIn
{
    [ComVisible(true)]
    public class ContextMenuRibbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public ContextMenuRibbon()
        {
        }        
        public bool IsVisible(Office.IRibbonControl control)
        {
            //string foldername = ((Microsoft.Office.Interop.Outlook.Folder)control.Context).Name;
            //if (foldername == "Inbox")
            //{
            //  return false;
            //}
            return true;
        }
        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("Ada.Tip.OutlookAddIn.contextMenuRibbon.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1
        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
            Globals.ThisAddIn.RibbonUI = ribbonUI;
        }
        /// <summary>
        /// Event fires when user clicks in the ContextMenu on "Start Timer"
        /// </summary>
        /// <param name="control"></param>
        public void OnMyButtonClick(Office.IRibbonControl control)
        {
            Globals.ThisAddIn.dashboardUserControl.dashboard.StopCurrentWorkItem();

            // Determine subject of selected item
            string subject = string.Empty;
            Explorer explorer = Globals.ThisAddIn.app.ActiveExplorer();
            if (explorer != null && explorer.Selection != null && explorer.Selection.Count > 0)
            {
                object item = explorer.Selection[1];
                if (item is MailItem)
                {
                    MailItem mailItem = item as MailItem;
                    subject = mailItem.Subject;
                }
                if (item is TaskItem)
                {
                    TaskItem taskItem = item as TaskItem;
                    subject = taskItem.Subject;
                }
                if (item is AppointmentItem)
                {
                    AppointmentItem appointmentItem = item as AppointmentItem;
                    subject = appointmentItem.Subject;
                }
            }

            
        }
        #endregion

        #region Helpers
        
        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.