Error 4120 Bad Parameter

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.
 
J

Jezebel

I tried it, but can't replicate the problem. There must be something in the
editing you're doing.

May I tactfully suggest you set yourself some standards for declaring your
variables? Otherwise, sooner or later you'll spend a day tearing out your
hair tracking down a bug that turns out to be a declaration mistake. (For
example, in this lot, do you really mean oLastRow to be a variant?)

The coding standards normally insist on

a) one declaration per line
b) always specify the type explicitly (even for variants) -- don't use type
suffixes (like '&')
c) use a consistent prefixing rule. My own preference is for the first
letter to indicate scope (p for procedure, m for module, g for global); a
more common -- but to me, less useful -- practice is for the first letter to
indicate data type.


And don't put functional code in your error-handler. You *really* don't want
to throw errors from within the error-handler itself.
 
G

Greg

Jezebel,

Thanks for your look and the comments. I will try to do better. I
can't create that error now using my Word2003 at home. At work it
does'nt matter what the edit is, but provided I run the macro from the
VB editor first it never happens. It may have something to do with the
MACROBUTTON I have in the form. I will see if I can run the macro from
a keyboard shortcut or icon without error tomorrow.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top