vba 激活特定的功能区选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21139173/
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-12 01:23:56  来源:igfitidea点击:

Activate a particular Ribbon Tab

excelvbaexcel-vbaribbon

提问by Sean

I've seen lots of code for how to ActivateTab OnLoadbut can't get it to work dynamically... on the click of a worksheet button for example.

我已经看到了很多关于如何ActivateTab OnLoad但不能让它动态工作的代码......例如,单击工作表按钮。

There is a button on Sheet1 pointing at startHereConfigure

Sheet1 上有一个按钮指向startHereConfigure

<customUI onLoad="RibbonOnLoad"
    xmlns="http://schemas.microsoft.com/office/2009/07/customui">

Public Rib As IRibbonUI
Sub RibbonOnLoad(ribbon As IRibbonUI)

    Set Rib = ribbon

End Sub

Sub startHereConfigure()

    Rib.ActivateTab "Configure"

End Sub

I want to be able to call startHereConfigure (and other similar subs) to bring to the fore a particular Tab - how do I do this?

我希望能够调用 startHereConfigure(和其他类似的子程序)以突出特定选项卡 - 我该怎么做?

回答by Jorge

Add this line to the custom XML file:

将此行添加到自定义 XML 文件中:

<customUI onLoad="RibbonOnLoad" xmlns="http://schemas.microsoft.com/office/2009/07/customui">

Where "RibbonOnLoad" is the name of the function to be called upon starting Outlook.

其中“RibbonOnLoad”是启动 Outlook 时要调用的函数的名称。

Add this funciotn into a standard module:

将此功能添加到标准模块中:

Public Rib As IRibbonUI

Sub RibbonOnLoad(ribbon As IRibbonUI)
   Set Rib = ribbon    
   Rib.ActivateTab "Configure" ' Name of the tab to activate    
End Sub

回答by konahn

Jorge's answer is the normal way.

豪尔赫的回答是正常的方式。

I introduce 2 other work-arounds here.

我在这里介绍了另外 2 个变通方法。

One is moving your custom tab control before the Home Tab by adding 'insertBeforeMso' in your customui(14).XML.

一种是通过在 customui(14).XML 中添加“insertBeforeMso”将自定义选项卡控件移动到主页选项卡之前。

<tab id="id1" label="Tab1" insertBeforeMso="TabHome">

Your 'Tab1' is the first one, so it is activated first when opening your office file.

您的“Tab1”是第一个,因此在打开 Office 文件时首先激活它。

The other one is using SendKey method which is a little tricky. After adding onLoad macro in customui XML, you can add following lines in a module in VBE.

另一种是使用 SendKey 方法,这有点棘手。在 customui XML 中添加 onLoad 宏后,您可以在 VBE 的模块中添加以下行。

Sub onLoad(control As IRibbonUI)
    'control.ActivateTab "Tab1"
    Application.SendKeys "%Y%"   'Alt + Y + Alt key
End Sub

Here 'Y' key is your custom tab shortcut key and 'Alt' key is sent again after 'Y' key in order to prevent the Alt shortcut keys from appearing on ribbon menus.

这里的“Y”键是您的自定义选项卡快捷键,“Alt”键在“Y”键之后再次发送,以防止 Alt 快捷键出现在功能区菜单上。

回答by Spinner

Samuel Liew: I thought the way I answered was implicit enough. The following is the same expanded to be more explicit:

Samuel Liew:我认为我回答的方式已经足够含蓄了。以下是相同的扩展以更明确:

Sean: Unclear from the above if you got an answer or you solved it yourself. As, I can't see an answer and your last comment suggests you went off with false information.

肖恩:从上面不清楚你是得到了答案还是你自己解决了。因为,我看不到答案,您的最后一条评论表明您提供了虚假信息。

Certainly your code should work in 2010 office systems. I'm doing just that and activating a range of different tabs, based on targeted worksheet 'types' becoming active. And the tab activation code is just part of a general workbook events handling sub (i.e. called by application event handlers). And that has been operational since prior to you asking this question.

当然,您的代码应该适用于 2010 办公系统。我正在这样做并根据目标工作表“类型”变得活跃来激活一系列不同的选项卡。选项卡激活码只是一般工作簿事件处理子(即由应用程序事件处理程序调用)的一部分。在您提出这个问题之前,这一直在运作。

So the response from Siddharth Rout above about not being able to do this is invalid.

因此,Siddharth Rout 上面关于无法做到这一点的回应是无效的。

I'm assuming your Tab id is defined in the xml to match the id in your code above per:

我假设你的 Tab id 是在 xml 中定义的,以匹配上面代码中的 id:

<tab id="Configure" label="YOUR LABEL" >

I'm also assuming the rest of your xml is correct, and you can see the tab, just not be able to activate it.

我还假设您的 xml 的其余部分是正确的,并且您可以看到该选项卡,只是无法激活它。

If all that's the case, as to 'How to': You should just need to call that sub. In my case I do that on Workbook or Worksheet activated (on open or change of window). But any call mechanic (such as your in-sheet control) should work just as well.

如果是这种情况,至于“如何”:您只需要调用该子程序即可。在我的情况下,我在激活的工作簿或工作表上执行此操作(在打开或更改窗口时)。但是任何调用机制(例如您的表单内控件)都应该同样有效。

As an aside, I'd recommend using more obviously unique codes for ribbon control ids e.g. Using some leading chars from the overall function and the control type like 'xyzTab_Configure'. Just means if/when you add more, you have a explicit way to know which is which, and avoid any clashes.

顺便说一句,我建议为功能区控件 ID 使用更明显的唯一代码,例如使用来自整个函数的一些前导字符和像“xyzTab_Configure”这样的控件类型。只是意味着如果/当你添加更多时,你有一种明确的方式来知道哪个是哪个,并避免任何冲突。

That all covered: I have however run into an issue with office 2019. See: vba IRibbonUI.ActivateTab not working in Office 2019

这一切都涵盖了:但是,我遇到了 Office 2019 的问题。请参阅:vba IRibbonUI.ActivateTab 在 Office 2019 中不起作用