You will find something similar at
http://www.gmayor.com/word_vba_examples.htm
Based on that code, start by creating a module with the macro
(
http://www.gmayor.com/installing_macro.htm) as follows:
Private mstrFF As String
Sub RowComplete()
Dim oFld As FormFields
Dim i As Long
Dim sCount As Integer
Dim sRow As Variant
Dim sPassword As String
sPassword = "" 'Insert the password (if any), used to protect the form
between the quotes
With GetCurrentFF 'Establish which dropdown field is current
mstrFF = GetCurrentFF.name
End With
With ActiveDocument
Set oFld = .FormFields
sRow = Split(mstrFF, "Row") 'Get the number at the end of the bookmark
name
'Check if the document is protected and if so unprotect it
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=sPassword
End If
For i = 1 To .Tables(1).Columns.Count
If .FormFields("Col" & i & "Row" & sRow(1)).Type =
wdFieldFormCheckBox Then
If .FormFields("Col" & i & "Row" & sRow(1)).CheckBox.Value =
False Then
With oFld(mstrFF).Range.Rows(1).Range
.Cells.Shading.BackgroundPatternColor = wdColorAutomatic
End With
GoTo Protect
Exit For
End If
End If
If .FormFields("Col" & i & "Row" & sRow(1)).Type =
wdFieldFormTextInput Then
If Len(.FormFields("Col" & i & "Row" & sRow(1)).Result) = 0 Then
With oFld(mstrFF).Range.Rows(1).Range
.Cells.Shading.BackgroundPatternColor = wdColorAutomatic
End With
GoTo Protect
Exit For
End If
End If
Next i
With oFld(mstrFF).Range.Rows(1).Range
.Cells.Shading.BackgroundPatternColor = wdColorRed 'Set the colour
of the checked box
End With
Protect:
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=sPassword
End With
End Sub
Private Function GetCurrentFF() As Word.FormField 'Get the dropdown field
name
Dim rngFF As Word.Range
Dim fldFF As Word.FormField
Set rngFF = Selection.Range
rngFF.Expand wdParagraph
For Each fldFF In rngFF.FormFields
Set GetCurrentFF = fldFF
Exit For
Next
End Function
This is the macro that validates the contents of the check box and text form
fields. Next remove all the rows from the table except the first i.e. you
should have one 22 column x 1 row table with the cells filled with text
boxes or check boxes as required. Don't worry about the naming convention as
the next macro will take care of that. Do not use a header row in the table
unless you are willing to reset the numbering in the macros to take account
of the extra row. Put your heading text outside the table.
Run the following macro which will name the fields and apply the exit/entry
macros to the fields.
Sub NameRow()
Dim oTable As Table
Dim oCell As Range
Dim iCol As Integer
Dim i As Long
Dim sPassword As String
sPassword = "" 'password to protect/unprotect form
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
Set oTable = .Tables(1) 'Select the appropriate table
iCol = oTable.Columns.Count 'Record the last column number
For i = 1 To iCol 'Repeat for each column
Set oCell = oTable.Cell(1, i).Range 'process each cell in the row
oCell.FormFields(1).Select 'Select the first field in the cell
With Dialogs(wdDialogFormFieldOptions) 'and name it
.name = "Col" & i & "Row1" 'eg Col1Row1
.Exit = "RowComplete"
.Entry = "RowComplete"
.Execute 'apply the changes
End With
Next i
.Protect NoReset:=True, Password:=sPassword, _
Type:=wdAllowOnlyFormFields 'Reprotect the form
End With
End Sub
Finally you need to add as many rows to the table as you require. Run the
following macro as many times as necessary. You can always run it again if
you need to add another row later. The macro will add rows complete with
named fields and associate the first macro to the fields.
Sub AddRow()
Dim oTable As Table
Dim oRng As Range
Dim oCell As Range
Dim sResult As String
Dim iRow As Integer
Dim iCol As Integer
Dim CurRow As Integer
Dim i As Long
Dim sPassword As String
sPassword = "" 'password to protect/unprotect form
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
Set oTable = .Tables(1) 'Select the appropriate table
iRow = oTable.Rows.Count 'Record the last row number
iCol = oTable.Columns.Count 'Record the last column number
Set oLastCell = oTable.Cell(iRow, iCol).Range 'Record the last cell
sResult = oLastCell.FormFields(1).Result 'Get the value in the last
cell
Set oRng = oTable.Rows(iRow).Range 'Add the last row to a range
oRng.Copy 'Copy the row
oRng.Collapse wdCollapseEnd 'collapse the range to its end.
oRng.Select 'the end of the table
Selection.Paste 'Paste the row at the end of the table
CurRow = iRow + 1 'Record the new last row
For i = 1 To iCol 'Repeat for each column
Set oCell = oTable.Cell(CurRow, i).Range 'process each cell in the
row
oCell.FormFields(1).Select 'Select the first field in the cell
With Dialogs(wdDialogFormFieldOptions) 'and name it
.name = "Col" & i & "Row" & CurRow 'eg Col1Row2
.Execute 'apply the changes
End With
Next i
.Protect NoReset:=True, Password:=sPassword, _
Type:=wdAllowOnlyFormFields 'Reprotect the form
End With
End Sub
As you tab through the entries in a row if all the checdk boxes are checked
and there is content in the text fields the whole row isa coloured red. If
any of the text boxes are unchecked the whole row is uncoloured.
The same field naming convention is used throughout. All the fields are
named according to their position in the table eg "Col1Row3"
NOTE! The macro takes no account of dropdown form fields and will not work
in a table that has split or joined cells or rows.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>