How to have a VSTO Ribbon and Context Menu at the same time?

Shane Chin picture Shane Chin · Jul 11, 2011 · Viewed 9.8k times · Source

EDIT: The posters answer is correct except for it should read xmlns="http://schemas.microsoft.com/office/2009/07/customui" for the include. As a side effect the ribbon and context menu defined in an XML file will not work in Office 2007. If you need to add a context menu in 2007, use the now deprecated, and a context menu within the Outlook 2007 message window is NOT POSSIBLE.

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

I've created both a Ribbon and a Context menu, but I do not know how to deploy both of them at the same time.

My Ribbon XML looks something like this:

<?xml version="1.0" encoding="UTF-8"?>

<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">

<ribbon>

<tabs>

  <tab id="testTab" label="Test Label">

    <group id="testGroup" label="test">

      <button id="testButton" onAction="testAction" label="Test" size="large" 

          getImage ="GetCustomImage" screentip="Test Ribbon Functionality."/>         

    </group>      

  </tab>

 </tabs>

</ribbon>

</customUI>

Ribbon.cs has

public string GetCustomUI(string ribbonID)

{

  String ui = null;

  // Examine the ribbonID to see if the current item

  // is a Mail inspector.

  if (ribbonID == "Microsoft.Outlook.Mail.Read" ||

    ribbonID == "Microsoft.Outlook.Mail.Compose")

  {

    // Retrieve the customized Ribbon XML.

    ui = GetResourceText("WDCrypt2.Ribbon.xml") ;



  }

  return ui;

}

ContextMenu XML looks like (from a tutorial)

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">

 <contextMenus>

  <contextMenu idMso="ContextMenuText">

   <button idMso="FontDialog" visible="false" />

   <toggleButton id="MyToggle" label="My Toggle Button" />

   <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />

   <menuSeparator id="MySeparator" />

   <menu id="MySubMenu" label="My Submenu" >

    <button id="MyButton2" label="Button on submenu" />

   </menu>

   <gallery id="galleryOne" label="My Gallery">

    <item id="item1" imageMso="HappyFace" />

    <item id="item2" imageMso="HappyFace" />

    <item id="item3" imageMso="HappyFace" />

    <item id="item4" imageMso="HappyFace" />

   </gallery>

   <dynamicMenu id="MyDynamicMenu" label= "My Dynamic Menu" getContent="GetMyContent" />

  </contextMenu>

 </contextMenus>

</customUI>

With its cs file which looks like:

private Office.IRibbonUI ribbon;

public Ribbon2()
{
}

#region IRibbonExtensibility Members

public string GetCustomUI(string ribbonID)
{
  return GetResourceText("WDCrypt2.Ribbon2.xml");
}

The problem is to use either in my Addin Class I must:

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{

  return new Ribbon();

}

OR

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{

  return new Ribbon2(); //The Context Menu

}

But I cannot do both. How do I get both the context menu and the ribbon at the same time?

Edit: I would also like to refrain from using Application.ItemContextMenuDisplay, as this is officially deprecated by the API.

Answer

Jake Ginnivan picture Jake Ginnivan · Jul 12, 2011

You need to combine the two ribbon XML files, then have a single callback file:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<ribbon>
<tabs>
  <tab id="testTab" label="Test Label">
    <group id="testGroup" label="test">
      <button id="testButton" onAction="testAction" label="Test" size="large" 
          getImage ="GetCustomImage" screentip="Test Ribbon Functionality."/>         
    </group>      
  </tab>
 </tabs>
</ribbon>
<contextMenus>
  <contextMenu idMso="ContextMenuText">
   <button idMso="FontDialog" visible="false" />
   <toggleButton id="MyToggle" label="My Toggle Button" />
   <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
   <menuSeparator id="MySeparator" />
   <menu id="MySubMenu" label="My Submenu" >
    <button id="MyButton2" label="Button on submenu" />
   </menu>
   <gallery id="galleryOne" label="My Gallery">
    <item id="item1" imageMso="HappyFace" />
    <item id="item2" imageMso="HappyFace" />
    <item id="item3" imageMso="HappyFace" />
    <item id="item4" imageMso="HappyFace" />
   </gallery>
   <dynamicMenu id="MyDynamicMenu" label= "My Dynamic Menu" getContent="GetMyContent" />
  </contextMenu>
 </contextMenus>
</customUI>