创建命令按钮 Class - 对于 Word 命令按钮
Creating a Command Button Class - For Word Command Buttons
我正在尝试批量设置一些命令按钮属性。那就是尝试一次设置命令按钮的各种属性,而不是单独为每个命令按钮重复代码。
该文档有 30 多个命令按钮。
在 Class - 我把代码放在下面:
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
在 VBA 模块中 - 我将代码放在下面:
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快递?不幸的是我丢失了 link.
不幸的是,我不知道如何继续解决这个问题。
最终解决方案:蒂姆的代码完美运行。您还需要加载按钮
将以下代码放入ThisDocument
Private Sub Document_Open()
Call SetupButtons
End Sub
cMDButtonClass(简体)
Public WithEvents oBtn As CommandButton
Private Sub oBtn_Click()
MsgBox "clicked: " & oBtn.Caption
End Sub
在常规模块中:
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
我正在尝试批量设置一些命令按钮属性。那就是尝试一次设置命令按钮的各种属性,而不是单独为每个命令按钮重复代码。
该文档有 30 多个命令按钮。
在 Class - 我把代码放在下面:
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
在 VBA 模块中 - 我将代码放在下面:
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快递?不幸的是我丢失了 link.
不幸的是,我不知道如何继续解决这个问题。
最终解决方案:蒂姆的代码完美运行。您还需要加载按钮
将以下代码放入ThisDocument
Private Sub Document_Open()
Call SetupButtons
End Sub
cMDButtonClass(简体)
Public WithEvents oBtn As CommandButton
Private Sub oBtn_Click()
MsgBox "clicked: " & oBtn.Caption
End Sub
在常规模块中:
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