0

I have created a new Excel application:

Microsoft.Office.Interop.Excel.Application application = new Excel.Application();

Now, I would like to hide the ribbon in this application completely (i.e. without giving to an Excel user a possibility to show this ribbon again). How can I do it?

I have found one solution:

application.ExecuteExcel4Macro("SHOW.TOOLBAR(\"Ribbon\",False)");

but I would like to find a more elegant way, not using macros. Is it somehow possible?

Jordan
  • 585
  • 1
  • 7
  • 18

3 Answers3

0

Try this:

If Application.ExecuteExcel4Macro("Get.ToolBar(7,""Ribbon"")") Then
    Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", False)"
Else
    Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", True)"
End If

...from another source. Don't know how to do it without a macro.

Brian
  • 2,078
  • 1
  • 15
  • 28
0

You may try to use Microsoft Active Accessibility.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

My solution is not so elegant, but it does what you want without Macro.

You need the following references:

  • using Microsoft.Office.Interop.Excel;
  • using System.Runtime.InteropServices;
  • using System.Diagnostics;
  • using System.Diagnostics;

Add this code to your class:

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    private static void OpenExcelWithoutRibbon() {

        Application excelApp = new Application();
        excelApp.Visible = true;

        Process prc = Process.GetProcessesByName("EXCEL").First();

        prc.WaitForInputIdle();
        IntPtr h = prc.MainWindowHandle;
        SetForegroundWindow(h);
        System.Windows.Forms.SendKeys.SendWait("^{F1}");            
    }

On my PC the Excel runs under process name "EXCEL", maybe it can differ on other PCs (like "excel" or "Excel"). Check it if does not finds it.

  • Sorry I made a typo, the last reference you need is the System.Windows.Forms ;) – Attila Maraz Apr 29 '16 at 13:44
  • The Excel closes immediately after the method finishes, so when you testing the code add a Console.ReadKey(); or similar to prevent it from closing. – Attila Maraz Apr 29 '16 at 13:49
  • Thank you, Attila. But in this case an Excel user will be able to return the ribbon back by pressing Ctrl+F1, but such a behavior is not desirable (the ribbon has to be hidden forever). – Jordan Apr 30 '16 at 08:39