njm njm - 2 days ago 3
C# Question

VSTO - Is it possible to have both designer and XML ribbons?

I'm working on an Outlook 2010 add-in that has multiple ribbons created with the Visual Studio 2010 ribbon designer. I've made an additional XML ribbon (I needed to override the default behavior of some built in ribbon buttons, which can't be done with the designer).

Enabling the XML ribbon disables all the designer ribbons because I have to override CreateRibbonExtensibilityObject (ContactButtonOverrides is my XML ribbon):

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new ContactButtonOverrides();
}


The base class implementation I'm overriding "returns a RibbonManager object that represents all Ribbon (Visual Designer) items in the project".

Is there any way to use both XML ribbons and ribbons made with the designer? Is there some way of adding my XML ribbon to the default RibbonManager?

Answer

There is no way to use both Ribbon Designer and Ribbon XML unless you use (2) separate add-ins. The IAddInExtension.CreateRibbonExtensibilityObject is only called once for the life cycle of your add-in. You can either choose to implement the interface using the Designer or the XML route. They are two separate API hooks.

The Ribbon Designer is more of a crutch for those new to Office Ribbon development. Once you get familiar with the Ribbon XML approach - it is much simpler and you have much more control over the behaviors (as you indicate in your OP). You can migrate from Ribbon Designer to XML using the Context Menu from the Ribbon Designer surface - although there is some rework for imagery and callbacks because the model is entirely different. It is worth your time to invest in Ribbon XML as it is the only way to extend Context Menus (CommandBars are deprecated) and Backstage View as there is no designer.

Ribbon XML Route

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        return new ContactButtonOverrides();
    }

Then within the ContactButtonOverrides...you can trigger which XML to load via IRibbonExtensibility.GetCustomUI which passes in the Ribbon ID Type...

public string GetCustomUI(string ribbonID)
{
    switch (ribbonID)
    {
        case "Microsoft.Outlook.Appointment" : 
            return GetResourceText("OutlookRibbonApp.IPM.Appointment.Ribbon.xml");
        case "Microsoft.Outlook.Mail.Compose" : 
            return GetResourceText("OutlookRibbonApp.IPM.Note.Ribbon.xml");
        default:
            return "";
    }
}

Ribbon Designer Route

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
    if (myCondition == true)
    {
        return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
            new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon1() });
    }
    else
    {
        return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
            new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon2() });
    }
}
Comments