VBA:如何遍历标签(不在用户窗体上)?
VBA: How to loop through labels (not on a userform)?
我的 Word 文档有很多 ActiveX 标签。 [不是文本框:我原来的标题有误。]
我想要一个宏来遍历它们以对它们中的每一个执行操作(更改标题),但我不知道如何识别它们。
如果他们在用户表单上,我会说:
For each aLabel in UserForm1.Controls
但这不适用于我的情况。
假设您正在使用的是文本框,根据标题而不是问题,文档的形状 collection 可能就是您想要的:
Sub ShapeLoop()
Dim shp As Shape
For Each shp In ThisDocument.Shapes
' Test if shp is one you're interesed in, perhaps using shp.Name
Debug.Print shp.Name
' Do Stuff
Next shp
End Sub
编辑:
字段也一样collection
Sub FieldLoop()
Dim fld As Field
For Each fld In ThisDocument.Fields
If TypeName(fld.OLEFormat.Object) = "Label" Then
Debug.Print fld.OLEFormat.Object.Caption
fld.OLEFormat.Object.Caption = "New Caption"
End If
Next
End Sub
我的 Word 文档有很多 ActiveX 标签。 [不是文本框:我原来的标题有误。]
我想要一个宏来遍历它们以对它们中的每一个执行操作(更改标题),但我不知道如何识别它们。
如果他们在用户表单上,我会说:
For each aLabel in UserForm1.Controls
但这不适用于我的情况。
假设您正在使用的是文本框,根据标题而不是问题,文档的形状 collection 可能就是您想要的:
Sub ShapeLoop()
Dim shp As Shape
For Each shp In ThisDocument.Shapes
' Test if shp is one you're interesed in, perhaps using shp.Name
Debug.Print shp.Name
' Do Stuff
Next shp
End Sub
编辑:
字段也一样collection
Sub FieldLoop()
Dim fld As Field
For Each fld In ThisDocument.Fields
If TypeName(fld.OLEFormat.Object) = "Label" Then
Debug.Print fld.OLEFormat.Object.Caption
fld.OLEFormat.Object.Caption = "New Caption"
End If
Next
End Sub