G
Greg
Hello,
Today I am trying to write a procedure that will tally check boxes in a
table. I achieved my basic goal but want to come up with a more
universal method.
I thought I would simple evaluate each cell in each column of a table
and if the cell contained a checked box I would increment a counter and
put the result of that counter in the last cell of the column.
I knew that cells that did not contain a formfield or a checkbox would
generate an error. I thought I could work around that with an error
handler.
While it appears completely illogical, after the first error, the code
is incrementing the counter on cells that don't contain a checkbox. To
see this weird behaviour, you will need a Three column table with
several rows.
The first row is a header row e.g., Do you like? Yes No
The first column you can leave blank
In Columns 2 and 3 but check boxes leaving the last row blank
Check one or tow boxes in both columns
protect the form and run:
Sub WierdBehaviour()
Dim oTbl As Word.Table
Dim oCol As Column
Dim oCell As Cell
Dim i As Integer
Set oTbl = ActiveDocument.Tables(1)
ActiveDocument.Unprotect
For Each oCol In oTbl.Columns
i = 0
For Each oCell In oCol.Cells
On Error GoTo Handler
MsgBox oCell.RowIndex & " " & oCell.ColumnIndex
If oCell.Range.FormFields(1).CheckBox.Value = True Then
i = i + 1
End If
Skip:
Next oCell
Next oCol
ActiveDocument.Protect wdAllowOnlyFormFields, noReset:=True
Exit Sub
Handler:
Resume Skip
End Sub
As you will see, the cells in the first column are incrementing the
counter even though there is no formfield in those cells.
I must be missing something in the Error Handling but can't crack this
nut.
Thanks.
Today I am trying to write a procedure that will tally check boxes in a
table. I achieved my basic goal but want to come up with a more
universal method.
I thought I would simple evaluate each cell in each column of a table
and if the cell contained a checked box I would increment a counter and
put the result of that counter in the last cell of the column.
I knew that cells that did not contain a formfield or a checkbox would
generate an error. I thought I could work around that with an error
handler.
While it appears completely illogical, after the first error, the code
is incrementing the counter on cells that don't contain a checkbox. To
see this weird behaviour, you will need a Three column table with
several rows.
The first row is a header row e.g., Do you like? Yes No
The first column you can leave blank
In Columns 2 and 3 but check boxes leaving the last row blank
Check one or tow boxes in both columns
protect the form and run:
Sub WierdBehaviour()
Dim oTbl As Word.Table
Dim oCol As Column
Dim oCell As Cell
Dim i As Integer
Set oTbl = ActiveDocument.Tables(1)
ActiveDocument.Unprotect
For Each oCol In oTbl.Columns
i = 0
For Each oCell In oCol.Cells
On Error GoTo Handler
MsgBox oCell.RowIndex & " " & oCell.ColumnIndex
If oCell.Range.FormFields(1).CheckBox.Value = True Then
i = i + 1
End If
Skip:
Next oCell
Next oCol
ActiveDocument.Protect wdAllowOnlyFormFields, noReset:=True
Exit Sub
Handler:
Resume Skip
End Sub
As you will see, the cells in the first column are incrementing the
counter even though there is no formfield in those cells.
I must be missing something in the Error Handling but can't crack this
nut.
Thanks.