vba - Word 2016 for mac - 任何类型的自定义控件

标签 vba macos ms-word ms-office

我有一个 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/

相关文章:

excel - VBA,查找MIN值,根据该值突出显示行

cocoa - 以编程方式强制 Cocoa 应用程序以 32 位模式启动

c - 当结构未正确对齐时,如何访问结构的成员?

c# - 由 C# 创建的 Word 文档中的表格和单元格 - 如何仅更改一个边框的颜色?

java - 使用 Java : replace string in MS Word file

excel - 将格式化数字的范围转换为文本字符串(粘贴时保持格式,但转换为文本)

mysql - Null VBA Excel 的使用无效

vba - 将事件处理程序分配给在 VBA 中动态创建的用户窗体上的控件

python - 为什么 python 在 Ubuntu 和 MacOS 上解析日期的方式不同?

VBA从文档中的数据创建另存为文件名