我在Word中有一个自定义功能区选项卡,使用CustomUI编辑器创建,有一系列按钮都可以正常工作。我试图找出是否可以在打开Word文档时设置此自定义选项卡的焦点。 [或者在创建新文档时。]
目前,它默认只显示“主页”选项卡。我找到了打开和关闭自定义标签的方法,但没有找到“设置焦点”的方法。
有什么想法吗?
答案 0 :(得分:3)
您可以通过Microsoft Active Accessibility实现此目的,我已经包含了以下网页中的代码,但如果您想了解更多信息,请检查是否有。 (样本位于页面底部)。
http://www.wordarticles.com/Shorts/RibbonVBA/RibbonVBADemo.php
在新模块中,您拥有接受accessibilty API的代码:
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' Definitions and Procedures relating to Accessibility, used by the Ribbon VBA '
' Demonstration UserForm. The constants have been lifted from oleacc.h, and are '
' just a subset of those available. '
' '
' Tony Jollans, August 2008. '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
Option Explicit
Public Const CHILDID_SELF As Long = &H0&
Public Const STATE_SYSTEM_UNAVAILABLE As Long = &H1&
Public Const STATE_SYSTEM_INVISIBLE As Long = &H8000&
Public Const STATE_SYSTEM_SELECTED As Long = &H2&
Public Enum RoleNumber
ROLE_SYSTEM_CLIENT = &HA&
ROLE_SYSTEM_PANE = &H10&
ROLE_SYSTEM_GROUPING = &H14&
ROLE_SYSTEM_TOOLBAR = &H16&
ROLE_SYSTEM_PAGETAB = &H25&
ROLE_SYSTEM_PROPERTYPAGE = &H26&
ROLE_SYSTEM_GRAPHIC = &H28&
ROLE_SYSTEM_STATICTEXT = &H29&
ROLE_SYSTEM_TEXT = &H2A&
ROLE_SYSTEM_BUTTONDROPDOWNGRID = &H3A&
ROLE_SYSTEM_PAGETABLIST = &H3C&
End Enum
Private Enum NavigationDirection
NAVDIR_FIRSTCHILD = &H7&
End Enum
Private Declare Function AccessibleChildren Lib "oleacc.dll" _
(ByVal paccContainer As Object, ByVal iChildStart As Long, ByVal cChildren As Long, _
rgvarChildren As Variant, pcObtained As Long) _
As Long
Public Function GetAccessible _
(Element As IAccessible, _
RoleWanted As RoleNumber, _
NameWanted As String, _
Optional GetClient As Boolean) _
As IAccessible
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' This procedure recursively searches the accessibility hierarchy, starting '
' with the element given, for an object matching the given name and role. '
' If requested, the Client object, assumed to be the first child, will be '
' returned instead of its parent. '
' '
' Called by: RibbonForm procedures to get parent objects as required '
' Itself, recursively, to move down the hierarchy '
' Calls: GetChildren to, well, get children. '
' Itself, recursively, to move down the hierarchy '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
Dim ChildrenArray(), Child As IAccessible, ndxChild As Long, ReturnElement As IAccessible
If Element.accRole(CHILDID_SELF) = RoleWanted And Element.accName(CHILDID_SELF) = NameWanted Then
Set ReturnElement = Element
Else ' not found yet
ChildrenArray = GetChildren(Element)
If (Not ChildrenArray) <> True Then
For ndxChild = LBound(ChildrenArray) To UBound(ChildrenArray)
If TypeOf ChildrenArray(ndxChild) Is IAccessible Then
Set Child = ChildrenArray(ndxChild)
Set ReturnElement = GetAccessible(Child, RoleWanted, NameWanted)
If Not ReturnElement Is Nothing Then Exit For
End If ' Child is IAccessible
Next ndxChild
End If ' there are children
End If ' still looking
If GetClient Then
Set ReturnElement = ReturnElement.accNavigate(NAVDIR_FIRSTCHILD, CHILDID_SELF)
End If
Set GetAccessible = ReturnElement
End Function
Private Function GetChildren(Element As IAccessible) As Variant()
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' General purpose subroutine to get an array of children of an IAccessible '
' object. The returned array is Variant because the elements may be either '
' IAccessible objects or simple (Long) elements, and the caller must treat '
' them appropriately. '
' '
' Called by: GetAccessible when searching for an Accessible element '
' Calls: AccessibleChildren API '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
Const FirstChild As Long = 0&
Dim NumChildren As Long, NumReturned As Long, ChildrenArray()
NumChildren = Element.accChildCount
If NumChildren > 0 Then
ReDim ChildrenArray(NumChildren - 1)
AccessibleChildren Element, FirstChild, NumChildren, ChildrenArray(0), NumReturned
End If
GetChildren = ChildrenArray
End Function
然后在您的ThisTemplate
或ThisDocument
模块中
Option Explicit
Private Sub Document_New()
SwitchTab "MyTab"
End Sub
Private Sub Document_Open()
SwitchTab "MyTab"
End Sub
Private Sub SwitchTab(TabName As String)
Dim RibbonTab As IAccessible
'Get the Ribbon as an accessiblity object and the
Set RibbonTab = GetAccessible(CommandBars("Ribbon"), ROLE_SYSTEM_PAGETAB, TabName)
'If we've found the ribbon then we can loop through the tabs
If Not RibbonTab Is Nothing Then
'If the tab state is valid (not unavailable or invisible)
If ((RibbonTab.accState(CHILDID_SELF) And (STATE_SYSTEM_UNAVAILABLE Or _
STATE_SYSTEM_INVISIBLE)) = 0) Then
'Then we can change to that tab
RibbonTab.accDoDefaultAction CHILDID_SELF
End If
End If
End Sub
答案 1 :(得分:3)
提出的答案不必要地复杂。最简单的解决方案是Sam回答的一部分。
添加&#34; onLoad&#34; -callback属性,引用自定义XML的自定义UI根节点中的方法名称。 E.g:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<ribbon startFromScratch="false">
<tabs>
<tab id="customTab" label="Custom Tab">
</tab>
</tabs>
</ribbon>
</customUI>
然后,将具有正确签名的回调方法添加到文档中的模块。在此方法中,您可以访问IRibbonUI对象,该对象可用于激活选项卡 - 无论是自定义还是内置。以下示例激活id等于&#34; customTab&#34;:
的自定义选项卡Sub Ribbon_Load(ribbon As IRibbonUI)
ribbon.ActivateTab "customTab"
End Sub
答案 2 :(得分:1)
将自定义功能区选项卡绘制到功能区后,您可以将密钥发送到模型,如下所示:
Application.SendKeys "%keytosend%"
要找出需要发送的密钥,请打开工作簿并按Alt
。您会注意到选项卡上显示了关键图标。
例如,要加载Page Layout
标签,您可以像这样发送密钥:
Application.SendKeys "%P%"
注意:您可能需要告诉自定义功能区代码要调用哪个宏onLoad
。这可以使用Custom UI Editor
在XML中完成。加载包含自定义功能区代码的工作簿,然后将onLoad
属性添加到<customUI></customUI>
标记,如下所示:
添加后,您必须创建一个与您定义的onLoad
函数匹配的VBA函数(在本例中为ribbonLoad)。然后,您可以在该功能中发送相应的密钥:
Sub ribbonLoad(ctl As IRibbonUI)
Application.SendKeys "%Y1%"
End Sub
答案 3 :(得分:1)
我想通过简单地将所有代码复制到您工作的文档的模块中,对CuberChase的优秀answer做一个补充,它在32位实例上工作。 / p>
但是,当您处理64位实例时,需要更改一些代码。
取代私有声明功能......长期:
Private Declare Function AccessibleChildren Lib "oleacc.dll" _
(ByVal paccContainer As Object, ByVal iChildStart As Long, ByVal cChildren As Long, _
rgvarChildren As Variant, pcObtained As Long) _
As Long
您需要声明私有声明PtrSafe功能...作为LongPtr 。因此,该功能变为:
Private Declare PtrSafe Function AccessibleChildren Lib "oleacc.dll" _
(ByVal paccContainer As Object, ByVal iChildStart As Long, ByVal cChildren As Long, _
rgvarChildren As Variant, pcObtained As Long) _
As LongPtr
有关更多解释,另请参阅this website。
注意,我使用此代码在Excel 2016中 SolverStudio 工作。我被http://solverstudio.org/using-studio/
引用到此页面答案 4 :(得分:0)
我无法让Sendkeys解决方案足够可靠地工作。 Active Accessibility解决方案似乎有效,但需要比我更多的时间。我肯定会在以后审查这个方法。
我确实发现有一种显示自定义标签的方法,如果它是标签列表中的第一个。我使用了这些设置:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="customTab" insertBeforeMso="TabHome" label="CustomTabName">
感谢您的选择。非常感谢。