Sangram Nandkhile
Sangram Nandkhile

Reputation: 18202

Excel 2007 Minimize the Ribbon programatically but Not the menu bar

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);
          }

        }
    }

Upvotes: 7

Views: 1928

Answers (1)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31651

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.

Upvotes: 1

Related Questions