Trying to create a conditional field or cell in a Word form

J

jdfranklin

I'm trying to recreate in Word a form I was able to create in Excel. Is it
possible and, if so, how? Here's the behavior I wish to reproduce:

1) A cell has a list of numerical values. If the user selects a value
above a given cutoff point, the cell changes color.
2) In a Yes/No/Uncertain scenario, the cell changes to one color if the
user selects No, and to another color if the user selects Unknown.

I'm going to re-post in the Programming forum in case that's the more
appropriate choice.

Thank you very much.
 
M

macropod

Hi jdfranklin,

Here's some code you can use as the basis of whatever conditional formatting you want. It's designed to be called from an on-exit
macro attached to a formfield. I used a dropdown formfield for development & testing, but you could use a text formfield instead.
The code changes the shading of whatever cell's formfield name is passed to it.

Modify the Case statements to suit your needs. If you want to change a whole row or column, you can change the line:
if I change the line:
With .Tables(Tbl).Cell(Row, Col).Shading
to:
With .Tables(Tbl).Rows(Row).Shading
or:
With .Tables(Tbl).Columns(Col).Shading

Option Explicit

Sub Dropdown1Colour()
Call CondFormat("Dropdown1")
End Sub

Sub CondFormat(Fld As String)
Dim Tbl As Integer
Dim Row As Integer
Dim Col As Integer
Dim Val As String
Dim Pwd As String
Pwd = "Password"
With ActiveDocument
For Tbl = 1 To .Tables.Count
If (.FormFields(Fld).Range.Start >= ActiveDocument.Tables(Tbl).Range.Start) And _
(.FormFields(Fld).Range.End <= ActiveDocument.Tables(Tbl).Range.End) Then
Exit For
End If
Next Tbl
With .FormFields(Fld)
Row = .Range.Cells(1).RowIndex
Col = .Range.Cells(1).ColumnIndex
Val = .Result
End With
.Unprotect Password:=Pwd
With .Tables(Tbl).Cell(Row, Col).Shading
Select Case CInt(Val)
Case Is = 1
.BackgroundPatternColorIndex = wdGreen
Case Is = 2
.BackgroundPatternColorIndex = wdPink
Case Is = 3
.BackgroundPatternColorIndex = wdTeal
Case Is = 4
.BackgroundPatternColorIndex = wdYellow
Case Is = 5
.BackgroundPatternColorIndex = wdTurquoise
Case Else
.BackgroundPatternColorIndex = wdAuto
End Select
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd
End With
End With
End Sub
 
M

macropod

Oops,

Change the last 4 lines to:
End With
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd
End With
End Sub
 

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