如何获取MS Word表单字段复选框使用VBA关联文本及其值?

时间:2016-11-25 10:19:43

标签: vba word-vba word-automation

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

表单字段:

form

代码:

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

1 个答案:

答案 0 :(得分:0)

编辑:

默认情况下,添加FormField复选框时,它的书签(名称)为Check#,其中#从1开始连续到n。复制和粘贴是你与FormFields的朋友,所以如果你去那条路线就会发生以下两种情况之一,比如你的1000 FormFields:

  • 1:如果您不更改书签的值(例如,默认为Check1)并复制并粘贴1000次,则最终会得到1000张书签Check1的FormField。
  • 2:如果您更改了书签的值(例如改为A)并复制并过去了1000次,则只有第一个FormField保留书签A,而其余的书签则为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 是空的。

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

import java.util.Scanner;
public class ElementaryMath {

    private double num1;
    private double num2;
    private String opType;
    private int numCorrect;
    private int actualAns;

    ElementaryMath(double n1, double n2, String ot){
        num1=n1;
        num2=n2;
        opType=ot;
    }

    public void printQuestion(){
        System.out.println((int)num1 + " " + opType + " " + (int)num2);
    }

    public void checkAnswer(int ans, String op){

        if(op.equals("/")) {
            actualAns = (int) num1 / (int) num2;
            System.out.println("answer is " + actualAns +" "+ ans);
        }
        if(op.equals("*")){
            actualAns = (int) num1 * (int) num2;
            System.out.println("answer is " + actualAns + " " + ans);
        }

        if(actualAns == ans){
            numCorrect++;
        }

    }

    public String numCorr(){
        return ""+numCorrect+"";
    }

    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        double n1, n2;
        String op = "";
        int nq, qt, aq;

        System.out.println("Which option would you like?");
        System.out.println("1. Single Digit - One operation");
        System.out.println("2. Two Digit -  One operation.");
        System.out.println("3. Single Digit - Multiple Operations (Mixed operators)");
        System.out.println("");
        System.out.println("Please enter your option. (1, 2, or 3): ");
        qt = in.nextInt();
        if(qt==1 || qt==2) {
            System.out.println("Please enter which operator you would like to use: (type / or * )");
            op = in.next();
        }
        System.out.println("Enter the number of questions you would like: ");
        nq = in.nextInt();

        if(qt==1 || qt==2)
        {
        for(int i=0;i<nq;i++) {
            n1 = Math.random() * 9 + 1;
            n2 = Math.random() * 9 + 1;
            ElementaryMath a = new ElementaryMath(n1, n2, op);
            a.printQuestion();
            System.out.println("Please print your answer: ");
            aq = in.nextInt();
            a.checkAnswer(aq, op);
            if(i==(nq-1)) {
                System.out.println("You have successfully answered " + a.numCorr() + " out of " + nq + " questions correctly.");
                }
            }
        } 
    }
}