Form Fields - macro or formula to automatically change row color

B

Beth R.

I am making a Word form (word 2003) for my co-workers to use as a checklist.
There are about 22 columns in this table for different items they need to
complete for each of their jobs (each job will be a different row). There
are two text fields and the rest are check boxes. My supervisor wants it to
automatically turn the entire row a different color once each of the 22 items
has been completed so they know its finished and don't have to look at it
anymore. Is there a macro or something I can use to do this?
And if it won't work in Word as a form, would something like this work in
Excel?
 
D

Doug Robbins - Word MVP

It is going to be a bit tedious to set it up, but if you have a macro
containing the following code set to run on exit from every formfield in
each of the rows that you want to exhibit this behaviour, it will turn any
completed row green and if the user goes back an unchecks one of the items
or deletes the text from a text field, it will remove the green shading from
that row.

Dim i As Long
Dim flag As Long
flag = 0
With Selection.Rows(1)
For i = 1 To .Cells.Count
With .Cells(i).Range.FormFields(1)
If .Type = wdFieldFormTextInput Then
If Trim(.Result) <> "" Then
flag = flag + 1
End If
ElseIf .CheckBox.Value = True Then
flag = flag + 1
End If
End With
Next i
ActiveDocument.Unprotect
If flag = .Cells.Count Then
.Shading.BackgroundPatternColor = wdColorGreen
Else
.Shading.BackgroundPatternColor = wdColorWhite
End If
ActiveDocument.Protect wdAllowOnlyFormFields, noreset
End With



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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