Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

Error 4120 Bad Parameter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 27 Mar 2006 15:13 GMT
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.
Jezebel - 27 Mar 2006 22:43 GMT
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.

> What cause Error 4120?
>
[quoted text clipped - 80 lines]
> What is this bad parameter and how do I prevent it from happening.
> Thanks.
Greg - 27 Mar 2006 23:39 GMT
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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.