Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Outlook Mailitem ContextMenu and Ribbon do not work together (only separately)

I need to create an Outlook add-in where the ribbon will work on the toolbar and ContextMenu (right click on mailitem).

At the beginning, I did these two things separately in separate projects.

In 1 project I add Ribbon (visual design). After launching it works very well.

In the 2 project I want to do contextmenu for mail. In the class ThisAddIn.cs adds:

protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new OutlookAddInExtensibility();
        }

And I add class OutlookAddInExtensibility.cs:

[ComVisible(true)]
    public class OutlookAddInExtensibility : IRibbonExtensibility
    {
        public string GetCustomUI(string RibbonID)
        {
            return
                @"<?xml version=""1.0"" encoding=""UTF-8""?>
                    <customUI xmlns=""http://schemas.microsoft.com/office/2009/07/customui"">
                        <contextMenus>    
                            <contextMenu idMso=""ContextMenuMailItem"">
                                <button 
                                    id=""MyContextMenuMailItem""
                                    label=""My new button label""
                                    onAction=""RibbonMenuClick""
                                />
                            </contextMenu>  
                        </contextMenus>
                    </customUI>
                ";
        }

        public void RibbonMenuClick(IRibbonControl control)
        {
            MessageBox.Show("Show text");
        }

After launching it works very well.

Unfortunately, when I add these two things in one project - only ContectMenu works. The ribbon at the top does not show up.

Does anyone know the solution to this problem?

like image 828
erbeen Avatar asked Jan 24 '26 22:01

erbeen


2 Answers

You need to choose a single way for customizing the UI - export the ribbon UI designed in Visual Studio to a ribbon XML file and then combine them together. Don't forget to return an appropriate markup depending on the RibbonID parameter passed to the GetCustomUI method.

There are two ways for creating a custom UI in Office applications (in VSTO):

  1. Walkthrough: Create a custom tab by using the Ribbon Designer
  2. Walkthrough: Create a custom tab by using Ribbon XML

Due to the fact that VSTO ribbon designer doesn't provide and support all ribbon features you need to export the existing UI into XML and continue dealing with it.

See How to: Export a ribbon from the Ribbon Designer to Ribbon XML for more information.

like image 112
Eugene Astafiev Avatar answered Jan 27 '26 12:01

Eugene Astafiev


Thanks to @Eugene Astafiev's help I found a solution. Export RibbonVisualDesigner to XML. And adding code from ContextMenu to XML. Two in one:

<?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="ContextMenuMailItem">
      <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>
like image 20
erbeen Avatar answered Jan 27 '26 10:01

erbeen