且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBA:如何遍历标签(不在用户窗体上)?

更新时间:2023-12-01 23:16:10

假设是您正在使用的文本框,按照标题而非问题,文档的Shapes集合可能就是您想要的:

Assuming it is textboxes you're working with, per the title but not the question, the document's Shapes collection may be what you're after:

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

对于字段集合再次相同

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