Excel Sendkeys不起作用

我有下面的代码应该尽量减lessExcel 2010function区。 我已经在workbook_open事件代码中放置了:

 Dim iheight As Integer iheight = Application.CommandBars.Item("Ribbon").Height If iheight > 100 Then Application.SendKeys ("^{F1}") End If 

但是,当我运行它,它打开Excel的帮助! 我已经尝试删除括号,但得到相同的结果。

有任何想法吗?

干杯

我在这里疯狂地猜测,你正试图在VBA编辑器中testing这个代码? 通过点击播放button或按F5?

如果您在VBA编辑器中testing,这将不起作用,因为Ctrl + F1 显示帮助菜单。

你需要做的是从Excel里面调用例程,所以excel是活动窗口。 简单地创build一个形状,并将其分配给例程来testing,然后以任何你想要的方式调用它。

记住sendkeys作用,它只是模仿键盘

你可以丢掉括号,虽然他们不会有任何区别

我的testing代码,工作正常:

 Sub test6() Dim iheight As Integer iheight = Application.CommandBars.Item("Ribbon").Height If iheight > 100 Then Application.SendKeys ("^{F1}") End If End Sub Sub MinRibbon() Call test6 'assigned to a shape on an excel sheet End Sub 

你可以试着保存你的工作簿,然后closures它并打开它(因为你有这个设置在workbook_open上运行),我没有看到没有理由不工作

你可以尝试下面的方法来隐藏function区:

 Sub hideRibbonIfVisible() If RibbonVisibility = 0 Then CommandBars.ExecuteMso "MinimizeRibbon" End If End Sub Function RibbonVisibility() As Integer RibbonVisibility = (CommandBars("Ribbon").Controls(1).Height < 100) End Function