Search code examples
excelvstoexcel-addins

VSTO: How to add custom menu to Excel File menu


Using a ribbon component, I added a file menu, but it does not appear in Excel after execution, but it appears when I add a button.

1.Is it possible to add menu component to file menu? enter image description here

2.When I add the button it shows up under Add-ins, is it possible to show that before Info (pls refer the image)? enter image description here enter image description here


Solution

  • Here is one way to do it by using the Ribbon XML and inserting the custom tab after the TabRecent "Open" tab. The Microsoft Office Fluent User Interface Control Identifiers only goes up to Office 2016. I could not get TabInfo to work.

    Here are the references I found.

    Backstage References:

    • Home = PlaceTabHome

    • New = TabOfficeStart

    • Open = TabRecent

    • Info = TabInfo

    • Save = FileSave

    • Save As = TabSave

    • History = HistoryTab

    • Print = TabPrint

    • Share = TabShare, ShareDocument

    • Export = TabPublish

    • Publish = Publish2Tab

    • Close = FileClose

    • Account = TabHelp

    • Feedback = TabOfficeFeedback

    • Options = TabOptions, ApplicationOptionsDialog


    XML Ribbon Code

    <ribbon startFromScratch="false">
    </ribbon>
    <backstage>
      <tab id="grpMyMenu" label="Annotate" insertAfterMso="TabRecent" visible="true" firstColumnMaxWidth="500">
        <firstColumn>
          <group id="grpOne" label="Microsoft Snipping Tool">
            <primaryItem>
              <button
                    id="btnSnippingTool"
                    label="Snipping Tool"
                    onAction="OnAction"
                    getImage="GetButtonImage"
                    screentip="Microsoft Snipping Tool"
                    supertip="Snipping Tool is a screenshot utility included in Microsoft Windows. It can take screenshots of an open window, rectangular areas, a free-form area, or the entire screen."
                    />
            </primaryItem>
            <topItems>
              <layoutContainer id="layoutOne" layoutChildren="horizontal">
                <labelControl id="ebox2" label="Snipping Tool is a screenshot utility included in Microsoft Windows. It can take screenshots of an open window, rectangular areas, a free-form area, or the entire screen." />
              </layoutContainer>
            </topItems>
          </group>
          <group id="grpTwo" label="Microsoft Problem Steps Recorder" >
            <primaryItem>
              <button
                    id="btnProblemStepRecorder"
                    label="Record Steps"
                    onAction="OnAction"
                    getImage="GetButtonImage"
                    screentip="Microsoft Problem Steps Recorder"
                    supertip="Problem Steps Recorder or PSR records the actions you take on your computer which you can then send to the person or group helping you with your computer problem."
                    />
            </primaryItem>
            <topItems>
              <labelControl id="ebox3" label="Problem Steps Recorder or PSR records the actions you take on your computer which you can then send to the person or group helping you with your computer problem."/>
            </topItems>
          </group>
        </firstColumn>
      </tab>
    </backstage>
    

    C# Code

        public void OnAction(Office.IRibbonControl control)
        {
            string filePath = string.Empty;
            switch (control.Id)
            {
                case "btnSnippingTool":
                    if (System.Environment.Is64BitOperatingSystem)
                    {
                        filePath = @"C:\Windows\sysnative\SnippingTool.exe";
                    }
                    else
                    {
                        filePath = @"C:\Windows\system32\SnippingTool.exe";
                    }
                    System.Diagnostics.Process.Start(filePath);
                    break;
                case "btnProblemStepRecorder":
                    filePath = @"C:\Windows\System32\psr.exe";
                    System.Diagnostics.Process.Start(filePath);
                    break;
            }
        }
    
        public System.Drawing.Bitmap GetButtonImage(Office.IRibbonControl control)
        {
            switch (control.Id)
            {
                case "btnProblemStepRecorder":
                    return Properties.Resources.problem_steps_recorder;
                case "btnSnippingTool":
                    return Properties.Resources.snipping_tool;
                default:
                    return null;
            }
        }
    

    Screenshot of File Tab "Backstage"

    screenshot