Making a Custom Ribbon Appear Only for a Custom Outlook Form (Norm Estabrook)

Many of you have posted questions to the VSTO Forum about how to make custom tabs, groups and controls appear only in cases where the user opens a custom form in Outlook.

The reason why that is difficult to accomplish is because there is no way to specify the names of custom forms in the Ribbon Designer. You can only specify inspector window types.

So if I set the RibbonType property of the Ribbon to “Microsoft.Outlook.Mail.Compose”,  then the Ribbon controls appear for all new mail items including items that I create by using a custom mail form. So how can I make sure that the Ribbon controls only appear in my custom mail form?

One possible workaround is to show and hide controls based on the message class of an Outlook form. It seemed to work for me and here is how I did it.

In the Ribbon designer, set the Visible property of your custom controls to false. At this point, those controls will not appear in any Outlook form custom or otherwise.

Next, add a property to the ThisAddIn class that stores the name of a custom message class as follows:

private string strMessageClass = "";
public string MessageClass
{
    get
    {
        return strMessageClass;
    }
    set
    {
        strMessageClass = value;
    }
}

Create an event handler for the NewInspector event. This event is raised every time a new Outlook inspector is open. The item that opens in the inspector has a message class name. For example, if you open a standard mail item, the message class name of the item is “IPM.Note”. A custom form based on a standard email form might have the message class name “IPM.Note.MyCustomForm”.

In the event handler, set your custom string property to the message class name of the open item. Here is an example:

private Outlook.Inspectors inspectors;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    inspectors = this.Application.Inspectors;
    inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler
            (Inspectors_NewInspector);

}

void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
    Outlook.MailItem tmpMailItem = (Outlook.MailItem)Inspector.CurrentItem;
    if (tmpMailItem != null)
        MessageClass = tmpMailItem.MessageClass;
}

In the load event of your Ribbon class, check your custom string property to determine the message class of the item that is attempting to load your custom Ribbon. If the name matches the name of your custom message class, you can show the controls on your custom Ribbon. Here is an example:

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
    if (Globals.ThisAddIn.MessageClass == "IPM.Note.Norm")
    {
        group1.Visible = true;
        Globals.ThisAddIn.MessageClass = "";
    }
    

}