G
Greg
What cause Error 4120?
Combining some code discussed in my previous Two Questions post, I have
put together a macro to validate and tally columns of checkboxes in a
table. To duplicate, create a 4 column, 5 row table. Leave the first
and last rows blank and the first column blank. Put checkboxes in the
remaining cells:
The following macro is set to run from a macrobutton in the document:
Sub ValidateAndTally()
Dim pTable As Word.Table
Dim pFormField As FormField
Dim pColumnCount&, pColumn&, i&, oLastRow
Dim pCount() As Long
Dim pColumnFlags() As Boolean
Dim x As Long, y As Long, z As Long
Dim bValidator As Boolean
Set pTable = ActiveDocument.Tables(1)
pColumnCount = pTable.Columns.Count
oLastRow = pTable.Rows.Count
ReDim pCount(1 To pColumnCount)
ReDim pColumnFlags(1 To pColumnCount)
On Error GoTo Handler
ActiveDocument.Unprotect
For Each pFormField In pTable.Range.FormFields
pColumn = pFormField.Range.Cells(1).ColumnIndex
If pFormField.Type = wdFieldFormCheckBox Then
pColumnFlags(pColumn) = True
If pFormField.CheckBox.Value Then
pCount(pColumn) = pCount(pColumn) + 1
End If
End If
Next
bValidator = True
For x = 2 To pTable.Rows.Count - 1
With pTable.Rows(x)
z = 0
For y = 2 To pTable.Columns.Count
If .Cells(y).Range.FormFields(1).CheckBox.Value = True Then
z = z + 1
End If
Next y
Select Case z
Case Is = 0
MsgBox "You did not answer question " & x - 1 & "."
bValidator = False
Exit For
Case Is > 1
MsgBox "You checked more than one answer to question " & x - 1
& "."
bValidator = False
Exit For
End Select
End With
Next x
For i = 1 To pColumnCount
If pColumnFlags(i) Then
If bValidator Then
pTable.Cell(oLastRow, i).Range.Text = pCount(i)
Else
pTable.Cell(oLastRow, i).Range.Text = "Not Validated"
End If
End If
Next
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
Exit Sub
Handler:
If Err.Number = 4605 Then
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
Resume
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
When I get it working, it seems to work as expected. However, when I
unprotect and edit the form then reprotect, I almost always get errror
4120 bad parameter when I try to fire the macro with the macrobutton.
If I then go into the editor and run the macro it runs fine and
thereafter runs from the macrobutton without further problem.
What is this bad parameter and how do I prevent it from happening.
Thanks.
Combining some code discussed in my previous Two Questions post, I have
put together a macro to validate and tally columns of checkboxes in a
table. To duplicate, create a 4 column, 5 row table. Leave the first
and last rows blank and the first column blank. Put checkboxes in the
remaining cells:
The following macro is set to run from a macrobutton in the document:
Sub ValidateAndTally()
Dim pTable As Word.Table
Dim pFormField As FormField
Dim pColumnCount&, pColumn&, i&, oLastRow
Dim pCount() As Long
Dim pColumnFlags() As Boolean
Dim x As Long, y As Long, z As Long
Dim bValidator As Boolean
Set pTable = ActiveDocument.Tables(1)
pColumnCount = pTable.Columns.Count
oLastRow = pTable.Rows.Count
ReDim pCount(1 To pColumnCount)
ReDim pColumnFlags(1 To pColumnCount)
On Error GoTo Handler
ActiveDocument.Unprotect
For Each pFormField In pTable.Range.FormFields
pColumn = pFormField.Range.Cells(1).ColumnIndex
If pFormField.Type = wdFieldFormCheckBox Then
pColumnFlags(pColumn) = True
If pFormField.CheckBox.Value Then
pCount(pColumn) = pCount(pColumn) + 1
End If
End If
Next
bValidator = True
For x = 2 To pTable.Rows.Count - 1
With pTable.Rows(x)
z = 0
For y = 2 To pTable.Columns.Count
If .Cells(y).Range.FormFields(1).CheckBox.Value = True Then
z = z + 1
End If
Next y
Select Case z
Case Is = 0
MsgBox "You did not answer question " & x - 1 & "."
bValidator = False
Exit For
Case Is > 1
MsgBox "You checked more than one answer to question " & x - 1
& "."
bValidator = False
Exit For
End Select
End With
Next x
For i = 1 To pColumnCount
If pColumnFlags(i) Then
If bValidator Then
pTable.Cell(oLastRow, i).Range.Text = pCount(i)
Else
pTable.Cell(oLastRow, i).Range.Text = "Not Validated"
End If
End If
Next
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
Exit Sub
Handler:
If Err.Number = 4605 Then
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
Resume
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
When I get it working, it seems to work as expected. However, when I
unprotect and edit the form then reprotect, I almost always get errror
4120 bad parameter when I try to fire the macro with the macrobutton.
If I then go into the editor and run the macro it runs fine and
thereafter runs from the macrobutton without further problem.
What is this bad parameter and how do I prevent it from happening.
Thanks.