Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel 2007 Minimize the Ribbon programatically but Not the menu bar

Tags:

c#

excel

vsto

In excel 2007, we can just right click on ribbon and select 'Minimize the ribbon' & minimize it.

I have tried

Application.ExecuteExcel4Macro("show.toolbar(\"ribbon\",false)");

which hides the whole ribbonbut I do not wish to hide whole ribbon.

I have even tried

 Application.SendKeys("^{F1}", true);

but it's not reliable as sometimes it doesn't work properly.

enter image description here

Is there any way to do it using C# VSTO code ?

I read a lot about toggleribbon() function but couldn't find way to use it.

EDIT: There is way you can actually find if the ribbon is already minimized. i used

    Office.CommandBars cbs = null;
    cbs = Application.CommandBars;
    foreach (Office.CommandBar cb in cbs)
    {
       if (cb.Name == "Ribbon")
       {

         if (cb.Height > 90)
         {
            this.Application.ActiveWindow.Activate(); 
            //to get focus on current workbook so that sendkeys will work
            Application.SendKeys("^{F1}", true);
          }

        }
    }
like image 812
Sangram Nandkhile Avatar asked Sep 20 '12 14:09

Sangram Nandkhile


People also ask

How do I lock the menu bar in Excel?

at the top-right corner, and then click Show Tabs and Commands. This will lock the ribbon at the top of the Excel window where it belongs.


1 Answers

SendKeys CTRL+F1 works, but it seems to be a timing issue with its execution. The real issue is that you don't know when the Ribbon is actually loaded in Excel to trigger the behavior.

This code seemed to work reliably for me, but it truly depends on how quickly your add-ins load. You can also utilize Thread.Sleep() if needed.

 private void ThisAddIn_Startup(object sender, System.EventArgs e)
 {
   Task.Factory.StartNew(() => {
       //Thread.Sleep(1000); // optional
       Application.SendKeys("^{F1}");
   }, TaskCreationOptions.AttachedToParent);
 }

See related MSDN forum post regarding Ribbon load timing.

like image 107
SliverNinja - MSFT Avatar answered Oct 26 '22 13:10

SliverNinja - MSFT