none
Office VSTO How to add a Button to the Context Menu witch opens on right click? RRS feed

  • Question

  • Hello,

    is there a Documentation how i can add a Button to the Context Menu ? I want the Button to display everytime someone clicks rightclick on Office. How can i handle that?

    Code must be in C# please!

    Thanks guys for any help :)

    Thursday, December 1, 2016 1:06 PM

Answers

  • Hello,

    You need to use the Fluent UI for customizing context menus in Office. See Customizing Context Menus in Office 2010 for more information. Also you may find the Customizing Context Menus in All Versions of Microsoft Excel page helpful.

    Read more about the Fluent UI (aka Ribbon UI) in the following series of articles:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, December 1, 2016 3:35 PM


  • Hy thanks for your answer i found out a Solution how to handle what i want:

    1. Create a Ribbon with the Ribbon Designer to create Menu Buttons on Top of Office.
    2. Converted the Ribbon Designer to the XML And CS File. Right Click on the Ribbon and Click "export to xml"
    3. After i got the ribbon.xml i also got the ribbon.cs. Now i inserted this code to the "ThisAddin.cs"


    //Start the Menu protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() { return new Ribbon();

    }

       4. So and now i got it to work. Now i have a MainMenu with Buttons in my Ribbon of Office and i also have a Context Menu.

    But i have one Problem now!

    My Context Menu witch shows up on right click only shows up when im in the window where i can write a new Email. But i want to show it alltime. No matter what window im in?

    This is my ribbon.xml, what did i wrong in the context menu?:

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
        <ribbon>
            <tabs>
                <tab id="tab1" label="EBS Plugins">
                    <group id="group1" label="Email Push">
                        <button id="button1" onAction="button1_Click" label="Sende alle Emails zur App" size="large" />
                    </group>
                    <group id="group2" label="Sync Kalender">
                        <button id="button2" label="App Kalender sync mit Outlook Kalender" size="large" />
                    </group>
                </tab>
            </tabs>
        </ribbon>
     <contextMenus>
        <contextMenu idMso="ContextMenuText">
          <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
          <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>
        </contextMenu>
      </contextMenus>
    </customUI>

    Thanks for help!


    • Edited by lukasrein97 Friday, December 2, 2016 8:25 AM
    • Marked as answer by lukasrein97 Friday, December 2, 2016 9:26 AM
    Friday, December 2, 2016 8:20 AM
  • Ah found it out by myselfe:

    You need the ContextMenuID So finally my xml looks like this and it works

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
        <ribbon>
            <tabs>
                <tab id="tab1" label="EBS Plugins">
                    <group id="group1" label="Email Push">
                        <button id="button1" label="Sende alle Emails zur App" size="large" getImage="imageEmail_GetImage" onAction="click_senden"/>
                    </group>
                    <group id="group2" label="Sync Kalender">
                        <button id="button2" label="App Kalender sync mit Outlook Kalender" size="large" getImage="imageKalender_GetImage" onAction="click_kalender"/>
                    </group>
                </tab>
            </tabs>
        </ribbon>
     <contextMenus>
       <!-- Context Menu wenn eine Email ausgewählt wurde -->
        <contextMenu idMso="ContextMenuMailItem">
          <menu id="OutlookPushContextMenu" label="EBS Plugins" getImage="imageLogo_GetImage" insertBeforeMso="Copy">
            <button id="OutlookPushEmail" label="Sende ausgewählte Emails" getImage="imageEmail_GetImage" onAction="click_senden"/>
            <button id="OutlookPushKalender" label="App Kalender sync mit Outlook Kalender" getImage="imageKalender_GetImage" onAction="click_kalender"/>
          </menu>
        </contextMenu>
       <!-- Context Menu wenn mehrere Emails ausgewählt wurden -->
       <contextMenu idMso="ContextMenuMultipleItems">
         <menu id="OutlookPushContextMenu2" label="EBS Plugins" getImage="imageLogo_GetImage" insertBeforeMso="Copy">
           <button id="OutlookPushEmail2" label="Sende ausgewählte Emails" getImage="imageEmail_GetImage" onAction="click_senden"/>
           <button id="OutlookPushKalender2" label="App Kalender sync mit Outlook Kalender" getImage="imageKalender_GetImage" onAction="click_kalender"/>
         </menu>
       </contextMenu>
      </contextMenus>
    </customUI>

    • Marked as answer by lukasrein97 Friday, December 2, 2016 9:25 AM
    Friday, December 2, 2016 9:25 AM

All replies

  • Hello,

    You need to use the Fluent UI for customizing context menus in Office. See Customizing Context Menus in Office 2010 for more information. Also you may find the Customizing Context Menus in All Versions of Microsoft Excel page helpful.

    Read more about the Fluent UI (aka Ribbon UI) in the following series of articles:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, December 1, 2016 3:35 PM


  • Hy thanks for your answer i found out a Solution how to handle what i want:

    1. Create a Ribbon with the Ribbon Designer to create Menu Buttons on Top of Office.
    2. Converted the Ribbon Designer to the XML And CS File. Right Click on the Ribbon and Click "export to xml"
    3. After i got the ribbon.xml i also got the ribbon.cs. Now i inserted this code to the "ThisAddin.cs"


    //Start the Menu protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() { return new Ribbon();

    }

       4. So and now i got it to work. Now i have a MainMenu with Buttons in my Ribbon of Office and i also have a Context Menu.

    But i have one Problem now!

    My Context Menu witch shows up on right click only shows up when im in the window where i can write a new Email. But i want to show it alltime. No matter what window im in?

    This is my ribbon.xml, what did i wrong in the context menu?:

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
        <ribbon>
            <tabs>
                <tab id="tab1" label="EBS Plugins">
                    <group id="group1" label="Email Push">
                        <button id="button1" onAction="button1_Click" label="Sende alle Emails zur App" size="large" />
                    </group>
                    <group id="group2" label="Sync Kalender">
                        <button id="button2" label="App Kalender sync mit Outlook Kalender" size="large" />
                    </group>
                </tab>
            </tabs>
        </ribbon>
     <contextMenus>
        <contextMenu idMso="ContextMenuText">
          <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
          <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>
        </contextMenu>
      </contextMenus>
    </customUI>

    Thanks for help!


    • Edited by lukasrein97 Friday, December 2, 2016 8:25 AM
    • Marked as answer by lukasrein97 Friday, December 2, 2016 9:26 AM
    Friday, December 2, 2016 8:20 AM
  • Ah found it out by myselfe:

    You need the ContextMenuID So finally my xml looks like this and it works

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
        <ribbon>
            <tabs>
                <tab id="tab1" label="EBS Plugins">
                    <group id="group1" label="Email Push">
                        <button id="button1" label="Sende alle Emails zur App" size="large" getImage="imageEmail_GetImage" onAction="click_senden"/>
                    </group>
                    <group id="group2" label="Sync Kalender">
                        <button id="button2" label="App Kalender sync mit Outlook Kalender" size="large" getImage="imageKalender_GetImage" onAction="click_kalender"/>
                    </group>
                </tab>
            </tabs>
        </ribbon>
     <contextMenus>
       <!-- Context Menu wenn eine Email ausgewählt wurde -->
        <contextMenu idMso="ContextMenuMailItem">
          <menu id="OutlookPushContextMenu" label="EBS Plugins" getImage="imageLogo_GetImage" insertBeforeMso="Copy">
            <button id="OutlookPushEmail" label="Sende ausgewählte Emails" getImage="imageEmail_GetImage" onAction="click_senden"/>
            <button id="OutlookPushKalender" label="App Kalender sync mit Outlook Kalender" getImage="imageKalender_GetImage" onAction="click_kalender"/>
          </menu>
        </contextMenu>
       <!-- Context Menu wenn mehrere Emails ausgewählt wurden -->
       <contextMenu idMso="ContextMenuMultipleItems">
         <menu id="OutlookPushContextMenu2" label="EBS Plugins" getImage="imageLogo_GetImage" insertBeforeMso="Copy">
           <button id="OutlookPushEmail2" label="Sende ausgewählte Emails" getImage="imageEmail_GetImage" onAction="click_senden"/>
           <button id="OutlookPushKalender2" label="App Kalender sync mit Outlook Kalender" getImage="imageKalender_GetImage" onAction="click_kalender"/>
         </menu>
       </contextMenu>
      </contextMenus>
    </customUI>

    • Marked as answer by lukasrein97 Friday, December 2, 2016 9:25 AM
    Friday, December 2, 2016 9:25 AM