Vba 为Word命令按钮创建命令按钮类

Vba 为Word命令按钮创建命令按钮类,vba,button,command,ms-word,Vba,Button,Command,Ms Word,我正在尝试批量设置一些命令按钮属性。这是尝试一次性设置命令按钮的各种属性,而不是单独重复每个命令按钮的代码 该文档有30多个命令按钮 在类中-我已将代码放在下面: Option Explicit Public WithEvents cMDButtonGroup As CommandButton Private Sub cMDButtonGroup_Click() With cMDButtonGroup If .Caption = "Press" Then ' Add

我正在尝试批量设置一些命令按钮属性。这是尝试一次性设置命令按钮的各种属性,而不是单独重复每个命令按钮的代码

该文档有30多个命令按钮

类中-我已将代码放在下面:

Option Explicit

Public WithEvents cMDButtonGroup As CommandButton

Private Sub cMDButtonGroup_Click()
With cMDButtonGroup

    If   .Caption = "Press" Then

    '  Add some other button properties

    Else

        .Caption = " Complete"


    End If
End With
Option Explicit
Dim Buttons() As New cMDButtonClass

Sub Buttons()
Dim ButtonCount As Integer
Dim ctl As Control

'   Create the Button objects
ButtonCount = 0
For Each ctl In ActiveDocument.Controls   ' This may be wrong
    If TypeName(ctl) = "CommandButton" Then

            ButtonCount = ButtonCount + 1
            ReDim Preserve Buttons(1 To ButtonCount)
            Set Buttons(ButtonCount).ButtonGroup = ctl
        End If
    End If
Next ctl

End Sub
VBA模块中-我已将代码放在下面:

Option Explicit

Public WithEvents cMDButtonGroup As CommandButton

Private Sub cMDButtonGroup_Click()
With cMDButtonGroup

    If   .Caption = "Press" Then

    '  Add some other button properties

    Else

        .Caption = " Complete"


    End If
End With
Option Explicit
Dim Buttons() As New cMDButtonClass

Sub Buttons()
Dim ButtonCount As Integer
Dim ctl As Control

'   Create the Button objects
ButtonCount = 0
For Each ctl In ActiveDocument.Controls   ' This may be wrong
    If TypeName(ctl) = "CommandButton" Then

            ButtonCount = ButtonCount + 1
            ReDim Preserve Buttons(1 To ButtonCount)
            Set Buttons(ButtonCount).ButtonGroup = ctl
        End If
    End If
Next ctl

End Sub
以上内容可能来自VBA Express?不幸的是,我失去了链接

不幸的是,我不知道如何解决这个问题

最终解决方案:Tim的代码运行良好。您还需要加载按钮

将以下代码放入此文档中

Private Sub Document_Open()

Call SetupButtons


End Sub
cMDButtonClass(简化)

在常规模块中:

Dim colButtons As New Collection '< simpler to manage than an array

Sub SetupButtons()
    Dim ButtonCount As Integer
    Dim ctl, c
    Dim oB As cMDButtonClass

    'Following Cindy's comment...
    For Each ctl In ActiveDocument.InlineShapes
        If Not ctl.OLEFormat Is Nothing Then
            Set c = ctl.OLEFormat.Object
            If TypeName(c) = "CommandButton" Then
                Set oB = New cMDButtonClass
                Set oB.oBtn = c
                colButtons.Add oB
            End If
        End If
    Next ctl

End Sub   
Dim colButtons As New Collection'<比数组更易于管理
子设置按钮()
Dim按钮计数为整数
Dim-ctl,c
将oB设置为cMDButtonClass
“在辛迪的评论之后。。。
对于ActiveDocument.InlineShapes中的每个ctl
如果不是ctl.OLEFormat,则为空
设置c=ctl.OLEFormat.Object
如果TypeName(c)=“CommandButton”,则
Set oB=New cMDButtonClass
设置oB.oBtn=c
colButtons.addob
如果结束
如果结束
下一个ctl
端接头
cMDButtonClass(简化版)

在常规模块中:

Dim colButtons As New Collection '< simpler to manage than an array

Sub SetupButtons()
    Dim ButtonCount As Integer
    Dim ctl, c
    Dim oB As cMDButtonClass

    'Following Cindy's comment...
    For Each ctl In ActiveDocument.InlineShapes
        If Not ctl.OLEFormat Is Nothing Then
            Set c = ctl.OLEFormat.Object
            If TypeName(c) = "CommandButton" Then
                Set oB = New cMDButtonClass
                Set oB.oBtn = c
                colButtons.Add oB
            End If
        End If
    Next ctl

End Sub   
Dim colButtons As New Collection'<比数组更易于管理
子设置按钮()
Dim按钮计数为整数
Dim-ctl,c
将oB设置为cMDButtonClass
“在辛迪的评论之后。。。
对于ActiveDocument.InlineShapes中的每个ctl
如果不是ctl.OLEFormat,则为空
设置c=ctl.OLEFormat.Object
如果TypeName(c)=“CommandButton”,则
Set oB=New cMDButtonClass
设置oB.oBtn=c
colButtons.addob
如果结束
如果结束
下一个ctl
端接头

Hi重复数据消除程序-这是word的VBA-还是我弄错了?这些是MSForms类型库中的ActiveX控件?为了“一般”地解决这些问题,您需要InlineShapes集合。测试“Type”属性。如果正确,则通过InlineShape.OLEObject.object获取控制对象。我现在使用的是移动设备,所以在明天之前不能给你任何更具体的信息…非常感谢Cindy,你是对的,它们是活动的X命令按钮-我期待你的建议-再次感谢!Hi Deduplicator-这是word的VBA-还是我弄错了?这些是MSForms类型库中的ActiveX控件?为了“一般”地解决这些问题,您需要InlineShapes集合。测试“Type”属性。如果正确,则通过InlineShape.OLEObject.object获取控制对象。我现在使用的是移动设备,所以在明天之前不能给你任何更具体的信息…非常感谢Cindy,你是对的,它们是活动的X命令按钮-我期待你的建议-再次感谢!嗨,蒂姆,谢谢你的密码。我已经设置了代码。当我按下命令按钮-没有消息弹出-我不知道为什么?蒂姆谢谢你的帮助,这段代码做得很好。还感谢Cindy的初始输入:)嗨,Tim,谢谢你的代码。我已经设置了代码。当我按下命令按钮-没有消息弹出-我不知道为什么?蒂姆谢谢你的帮助,这段代码做得很好。还感谢Cindy的初始输入:)