简体   繁体   中英

Word VBA: Status of a checkbox in Word table cells

I have a set of identical Word files containing a set of yes/no questions organized in a table with simple checkboxes (from the Developer tab / Content Controls) included in columns 3 and 5, corresponding to the Yes or No answer.

The Yes and No checkboxes are either checked or unchecked.

I want to count the number of Yes and No checks for each of the questions. The Word files are already available and their format cannot be changed.

As a first step in this task I am trying to recognize when a table cell contains a checkbox, and then read the status of the checkbox: checked or unchecked.

I came to the following code to identify the checkboxes. VBA complains with a compilation error: "Object Required", at line "set nitem.....".

Dim tbl As Table
Dim cl As Cell
Dim nitem As Integer

''All tables
For Each tbl In ActiveDocument.Tables
    For Each cl In tbl.Rows(1).Cells
        If Not cl.Range.ContentControls Is Nothing Then
            Set nitem = c1.Range.ContentControls.Count
            MsgBox ("Nr of Items" & nitem)
            If c1.Range.ContentControls.Item(1).Type = wdContentControlCheckBox Then
              ''Select ...
              cl.Column.Select
               .........
            End If
        End If
    Next
Next

The keyword "Set" is for objects. An Integer is not an object so your line needs to be just:

nitem = cl.Range.ContentControls.Count

without "Set"

EDIT: This would cause the error that you are referring to, but I also noticed another issue that needs to be fixed after your comment.

Note that the declared variable is "cl" where the second character is a lowercase "L". You have tried to use it as "c1" where the second character is the numeral "One" Make sure to use "CL" in all cases where this variable appears (in the line in question and also two lines below this)

If you use "Option Explicit" at the beginning of the "This_Workbook" module (or whatever module the code is in) before any Subroutines, this will help prevent misspelling errors like this as the compiler will raise an exception alerting you that the variable is not defined.

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