我有一个 Word 插件。在 office for windows 上,它能够使用功能区 XML 显示自定义功能区。在 office 2011 for mac 上,它会忽略此功能区,但我可以添加一个 CommandBar(顶部的下拉菜单)。这些允许我为我的各种宏创建一个菜单系统。
在 Office 2016 中,当我尝试创建命令栏时出现错误:"Method 'Add' of Object 'CommandBarControls' failed
。它也不允许自定义功能区,它不允许选择 dotm 文件中的 XML(它似乎也不允许我手动更改功能区)。
这让我想到了一个问题 - 有没有什么办法,我可以在某处添加一些按钮来在这个版本的 office 上触发宏?最好是我可以使用 VBA 创建的东西(目前命令栏是从 VBA 添加的)。
既然这适用于 Office for Windows(预览版)以及旧版本的 office for windows,以及 Office 2011 for mac - 为什么它不能在这里工作?
作为引用,这是我创建命令栏的测试代码,来自 http://scriptorium.serve-it.nl/view.php?sid=14
Sub CreateCommandBar()
Dim myCB As CommandBar
Dim myCBtn1 As CommandBarButton
Dim myCBtn2 As CommandBarButton
Dim myCPup1 As CommandBarPopup
Dim myCPup2 As CommandBarPopup
Dim myCP1Btn1 As CommandBarButton
Dim myCP1Btn2 As CommandBarButton
' Delete the commandbar if it exists already
' On Error Resume Next
Application.CommandBars("example").Delete
' Create a new Command Bar
Set myCB = CommandBars.Add(Name:="example", Position:=msoBarFloating)
' Add button 1 to this bar
Set myCBtn1 = myCB.Controls.Add(Type:=msoControlButton)
With myCBtn1
.Caption = "1st level Cap."
.Style = msoButtonCaption '<- will force the caption text to show on your button
End With
' Add popup menu 1 to this bar
Set myCPup1 = myCB.Controls.Add(Type:=msoControlPopup)
myCPup1.Caption = "Statistic"
' Add button 1 to popup menu 1
Set myCP1Btn1 = myCPup1.Controls.Add(Type:=msoControlButton)
With myCP1Btn1
.Style = msoButtonAutomatic
.FaceId = 487
End With
' Add button 2 to popup menu 1
Set myCP1Btn1 = myCPup1.Controls.Add(Type:=msoControlButton)
With myCP1Btn1
.Caption = "Click me!"
.Style = msoButtonIconAndCaption
.FaceId = 59
.OnAction = "SubItworks"
End With
' Add a second button to this bar
Set myCBtn2 = myCB.Controls.Add(Type:=msoControlButton)
With myCBtn2
.FaceId = 17 ' <- Face Id 17 is a barchart icon
.Caption = "Descriptive stat"
End With
' Show the command bar
myCB.Visible = True
End Sub
Sub SubItworks()
MsgBox ("Eureka, it works!")
End Sub
最佳答案
在 Word 2011 中创建一个新的 .dotm 文件,运行 CreateCommandBar() 宏,然后将工具栏(使用管理器对话框)和 SubItworks() 宏复制到 .dotm 文件。将 .dotm 文件放入 Word 2016 的启动文件夹中,您将在功能区的加载项选项卡中看到工具栏。
虽然它有问题 - 唯一似乎有效的控件是顶级按钮。单击弹出按钮不会显示菜单,当您单击它们时,甚至顶级按钮也无法正确绘制。
我在 Word 2011 中手动创建了一个新的工具栏( View :工具栏:自定义工具栏和菜单...),然后通过将宏拖动到工具栏来创建按钮。一旦获得对工具栏的引用,您还可以使用 Visual Basic 的本地窗口手动设置按钮属性。
只要您在运行时不更改任何按钮属性,以这种方式创建的按钮似乎会正常运行。
关于vba - Word 2016 for mac - 任何类型的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31381769/