VBA:如何遍历标签(不在用户窗体上)? [英] VBA: How to loop through labels (not on a userform)?

查看:285
本文介绍了VBA:如何遍历标签(不在用户窗体上)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Word文档具有许多ActiveX标签. [不是文本框:我的原​​始标题有误.]

My Word document has many ActiveX labels. [Not textboxes: my original title was in error.]

我想要一个宏来遍历它们以对它们中的每一个执行操作(更改标题),但是我不知道如何识别它们.

I'd like a macro to loop through them to perform an action on each of them (changing the captions), but I don't know how to identify them.

如果他们使用的是用户表单,我会说: For each aLabel in UserForm1.Controls

If they were on a userform, I'd say: For each aLabel in UserForm1.Controls

但这不适用于我的情况.

But that doesn't apply in my case.

推荐答案

假设是您正在使用的文本框,按照标题而非问题,文档的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

这篇关于VBA:如何遍历标签(不在用户窗体上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆