【问题标题】:How to get the MS Word Form Fields Check box associated text and their value using VBA?如何使用 VBA 获取 MS Word 表单字段复选框相关文本及其值?
【发布时间】:2016-11-25 10:19:43
【问题描述】:

如何获取 MS Word 文档复选框表单元素关联的文本值。我能够提取复选框的值。我尝试使用书签和名称属性,发现没有与复选框的书签字段关联的值。我得到以下输出。有什么想法吗?

表单字段:

代码:

Sub Test()
    Dim strCheckBoxName As String
    Dim strCheckBoxValue As String
    For i = 1 To ActiveDocument.FormFields.Count
        If ActiveDocument.FormFields(i).CheckBox Then
            strCheckBoxName = ActiveDocument.FormFields(i).Name
            strCheckBoxValue = ActiveDocument.FormFields(i).CheckBox.Value
            Debug.Print strCheckBoxName & " = " & strCheckBoxValue
        End If
    Next
End Sub

输出:

Check1 = True
Check1 = True
Check1 = True
Check1 = False
Check1 = False
Check1 = False

正在寻找的解决方案:

A = True
B = True
C = True
D = False
E = False
F = False

【问题讨论】:

  • 可能你应该使用strCheckBoxName = ActiveDocument.FormFields(i).CheckBox.Name,因为字段名称对所有人来说都是一样的。
  • 我试过了。但没有运气。它正在抛出 null。

标签: vba ms-word word-automation


【解决方案1】:

编辑:

默认情况下,添加 FormField 复选框时,它的书签(名称)为 Check#,其中 # 是从 1 开始到 n 的顺序。复制和粘贴是您与 FormFields 的朋友,因此如果您走这条路去获取,比如您的 1000 个 FormFields,将会发生以下两种情况之一:

  • 1:如果您不更改 Bookmark 的值(例如,默认为 Check1)并复制粘贴 1000 次,您最终会得到 1000 个 Bookmark Check1 的 FormFields。
  • 2:如果您更改 Bookmark 的值(例如更改为 A)并复制并过去 1000 次,则只有第一个 FormField 保留 A 的 Bookmark,而其余的 Bookmark 为空。

您可以将复选框的默认书签值(在这种情况下,Check1 是反复复制和粘贴的结果)更改为顺序值,例如 A1、A2、A3、A4 或 Check1、Check2、Check3 等。 ..通过使用以下:

Sub Test()
    Dim strCheckBoxName As String
    Dim strCheckBoxValue As String
    For i = 1 To ActiveDocument.formFields.Count
        If ActiveDocument.formFields(i).CheckBox Then
            strCheckBoxName = ActiveDocument.formFields(i).Name
            strCheckBoxValue = ActiveDocument.formFields(i).CheckBox.Value
            Debug.Print strCheckBoxName & " = " & strCheckBoxValue
        End If
    Next
End Sub

Sub RenameCheckBox()
    Dim oFields As formFields
    Dim oVar As Variant
    Dim i As Long
    Dim x As Long

    x = 0
    i = 0

    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If
    Set oFields = ActiveDocument.formFields
    For x = 1 To oFields.Count
        oFields(x).Select
        Select Case oFields(x).Type
            Case wdFieldFormCheckBox
                oVar = oFields(x).CheckBox.Value
                i = i + 1
                With Dialogs(wdDialogFormFieldOptions)
                    .Name = "Check" & i
                    .Execute
                End With
                oFields(x).CheckBox.Value = oVar
            Case Else
                'Do Nothing
        End Select
    Next x
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

    Call Test

End Sub

【讨论】:

  • 感谢您的回答。但是文档受值保护,并且文档超过 1000 个。
  • 如果这些复选框表单字段超过 1000 个,它们的命名约定是什么?
  • "我尝试了书签和名称属性,发现没有与复选框的书签字段关联的值。"你是如何检查书签属性的?使用 VBA 还是您取消了对文档的保护?
猜你喜欢
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2015-01-10
  • 2019-09-09
相关资源
最近更新 更多