Word VBA中用集合(Collection)遍历inlineshape控件
━━━━━━━━━━━━━━━━━━━━━━━━━
胡子注:在word中插入多个单选按钮制作选择题,问卷调查等很方便。但由于vba不支持控件数组,所以写代码很麻烦。网上有许多VBA玩家用类方法实现控件数组,我看了一下还是不简单,于是用集合试了一下,实在是太方便了!
Dim col As New Collection
Private Sub Document_Open()
col.Add OptionButton1
col.Add OptionButton2
col.Add OptionButton3
col.Add OptionButton4
col.Add OptionButton11
col.Add OptionButton21
col.Add OptionButton31
col.Add OptionButton41
col.Add OptionButton12
col.Add OptionButton22
col.Add OptionButton32
col.Add OptionButton42
End Sub
Private Sub Commanon2_Click() '清空所有选项
Dim op As OptionButton
For Each op In col
op.Value = False
Next
End Sub
Private Sub Commanon3_Click() '根据控件名称操作
Dim op As OptionButton
For Each op In col
MsgBox InStr(op.Name, 'OptionButton1')
If InStr(op.Name, 'OptionButton1') <> 0 Then
op.Value = True
End If
'MsgBox op.Name 'op.Name 为只读属性,可用
Next
End Sub