简体   繁体   中英

How to get the MS Word Form Fields Check box associated text and their value using VBA?

How to get the MS word document checkbox form element associated text value. I am able to extract the value of the checkbox. I tried with bookmark and name properties and found that there is no value associated with bookmark filed of the checkbox. I got the following output. Any thoughts?

Form Fields:

形成

Code:

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

Output:

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

Solution looking for:

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

EDIT:

By default, when a FormField Check Box is added, it has a Bookmark (name) of Check# where # is sequential starting at 1 until n . Copy and Paste are your friends with FormFields, so one of two things will occur if you go that route to get, say your 1000 FormFields:

  • 1: If you do not alter the value of Bookmark (eg default to Check1 ) and copy and paste that say 1000 times, you end up with 1000 FormFields of Bookmark Check1.
  • 2: If you alter the value of Bookmark (eg to A ) and copy and past that say 1000 times, only the first FormField retains the Bookmark of A while the rest have a Bookmark that is empty.

You can alter the Check Box default bookmark value (in this case Check1 as a result from copy and paste over and over) to a sequential value such as A1, A2, A3, A4 or Check1, Check2, Check3, etc... by using the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM