Excel 2007 Minimize Ribbon programmatically, but not in the menu bar

In excel 2007, we can simply right-click on a ribbon and select “Minimize Ribbon” and minimize it.

I tried

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

which hides the entire tape, but I do not want to hide the whole tape.

I even tried

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

but it is not reliable, because sometimes it does not work correctly.

enter image description here

Is there a way to do this using C # VSTO code?

I read a lot about the toggleribbon() function, but could not find a way to use it.

EDIT: There is a way you can really find if the tape 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); } } } 
+7
source share
1 answer

SendKeys CTRL + F1 works, but it seems to be a synchronization issue with its execution. The real problem is that you do not know when the ribbon is really loaded in Excel to trigger the behavior.

This code seemed to work reliably for me, but it really depends on how fast your add-ons load. You can also use Thread.Sleep() if necessary.

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

See the related MSDN forum post for feed loading times .

+1
source

All Articles