【发布时间】:2016-03-10 17:24:39
【问题描述】:
我正在尝试批量设置一些命令按钮属性。那是尝试一次性设置命令按钮的各种属性,而不是单独为每个命令按钮重复代码。
该文档有 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
在 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 Express?不幸的是,我丢失了链接。
很遗憾,我不知道如何解决这个问题。
最终解决方案:Tim 的代码运行良好。您还需要加载按钮
将以下代码放入ThisDocument
Private Sub Document_Open()
Call SetupButtons
End Sub
【问题讨论】:
-
嗨 Deduplicator - 这是 VBA for word - 还是我弄错了?
-
这些是 MSForms 类型库中的 ActiveX 控件吗?为了“一般”地解决它们,您需要 InlineShapes 集合。测试“类型”属性。如果这是正确的,那么您可以通过 InlineShape.OLEObject.Object 获得控制对象。我目前正在使用移动设备,所以直到明天才能给你更具体的信息......
-
非常感谢 Cindy,你说得对,它们是 Active X - 命令按钮 - 我期待你的建议 - 再次感谢!
标签: vba button command ms-word