VBA 最小化 Excel 中的功能区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19019546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:43:45  来源:igfitidea点击:

VBA minimize ribbon in Excel

excelvbaexcel-vba

提问by Kyle Weller

I want to minimize the ribbon in Excel 2013 with VBA. I do not want to toggle the ribbon, and I do not want to hide everything including "File", "Insert", etc. I have tried several different methods, but none satisfy what I want.

我想用 VBA 最小化 Excel 2013 中的功能区。我不想切换功能区,也不想隐藏包括“文件”、“插入”等在内的所有内容。我尝试了几种不同的方法,但都没有满足我的要求。

This hides everything:

这隐藏了一切:

Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"",False)

This toggles:

这切换:

CommandBars.ExecuteMso "MinimizeRibbon"

This also toggles:

这也切换:

SendKeys "^{F1}"

How can I simply force my ribbon to be minimized?

我怎样才能简单地强制最小化我的功能区?

采纳答案by engineersmnky

Not sure when you are trying to call this but this will work to minimize the ribbon if its open

不确定您何时尝试调用它,但是如果功能区打开,这将有助于最小化功能区

If Application.CommandBars("Ribbon").Height >= 150 Then
    SendKeys "^{F1}"
End If

Open Ribbon minimum size seems to be 150 so this will only toggle if it's open

打开功能区的最小尺寸似乎是 150,因此只有在打开时才会切换

回答by LawrieM

Measure Ribbon height, toggle it, measure again and if taller, re-toggle. Best also to set Application.Screenupdating = false.

测量色带高度,切换它,再次测量,如果更高,重新切换。最好也设置 Application.Screenupdating = false。

ht1 = Application.CommandBars("Ribbon").Height
SendKeys "^{F1}", False
DoEvents
ht2 = Application.CommandBars("Ribbon").Height
If ht2 > ht1 Then SendKeys "^{F1}", False

And I do hate it when folk question why you want to do what you want. I have a Dictator App and need 100% control over the interaction with Excel.

当人们质疑为什么你想做你想做的事时,我真的很讨厌它。我有一个独裁者应用程序,需要 100% 控制与 Excel 的交互。

回答by XLhacks

Provided no-one else is messing with the settings (and if everyone did what I'm to suggest it would be great), if you retoggle ribbon minimise before close, it will always be there on open so it CAN be minimised. Just make sure to manually hide the ribbon before exiting this one time, then is will always hide on open.

如果没有其他人弄乱设置(如果每个人都按照我的建议行事,那就太好了),如果您在关闭前重新切换色带最小化,它会一直在打开时出现,因此可以最小化。只要确保在退出这一次之前手动隐藏功能区,那么它就会始终隐藏在打开状态。

Private Sub Workbook_Open()

Application.DisplayFormulaBar = False
Application.CommandBars.ExecuteMso "MinimizeRibbon"

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Application.DisplayFormulaBar = True
Application.CommandBars.ExecuteMso "MinimizeRibbon"

End Sub

回答by RJSK

If you want this to be hidden completely as soon as the workbook is opened then add this to the workbook code:

如果您希望在工作簿打开后立即将其完全隐藏,请将其添加到工作簿代码中:

Private Sub Workbook_Open()
    Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
End Sub