A modified Doug Robbins macro

G

Greg Maxey

I was reading a New User posting earlier today and became interested in
creating a macro to change the text color of a dropdown field result. I
have a single dropdown field with the List Entries Red, Blue, Green. I came
across a macro that Doug Robbins provided me last year and modified it as
follows.

Sub ChangeFontColor()

Dim DropDown

DropDown = ActiveDocument.FormFields(1).DropDown.Value

If DropDown = 1 Then
With ActiveDocument
.FormFields(1).Select
.Sections(1).ProtectedForForms = False
Selection.Font.Color = wdColorRed
.Sections(1).ProtectedForForms = True
End With
End If
If DropDown = 2 Then
With ActiveDocument
.FormFields(1).Select
.Sections(1).ProtectedForForms = False
Selection.Font.Color = wdColorBlue
.Sections(1).ProtectedForForms = True
End With
End If
If DropDown = 3 Then
With ActiveDocument
.FormFields(1).Select
.Sections(1).ProtectedForForms = False
Selection.Font.Color = wdColorGreen
.Sections(1).ProtectedForForms = True
End With

End If

End Sub


Is this an efficient method? What would be more efficient if I had say 100
similiar dropdown fields in a large document? I mean how would I loop
through all dropdown fields in a document and set the font color based the
Value 1, 2, or 3 without having to create a 100 similiar macros?

As always thanks for all the help you provide.
 
P

Peter Hewett

Hi Greg

Give this a try it's a bit shorter:

Public Sub FFTest()
Dim ffItem As Word.FormField

' Select the current form field
Selection.Expand wdParagraph
Set ffItem = Selection.FormFields(1)

ActiveDocument.Unprotect
Select Case ffItem.DropDown.Value
Case 1
Selection.Font.Color = wdColorRed
Case 2
Selection.Font.Color = wdColorBlue
Case 3
Selection.Font.Color = wdColorGreen
End Select
ActiveDocument.Protect wdAllowOnlyFormFields
End Sub

HTH + Cheers - Peter
 
P

Peter Hewett

Hi All

Of course:
ActiveDocument.Protect wdAllowOnlyFormFields
should be:
ActiveDocument.Protect wdAllowOnlyFormFields, True

Cheers - Peter
 
J

Jay Freedman

Hi, Greg,

One key to solving the problem is that you can step through all the
members of a collection such as ActiveDocument.FormFields by using a
For Each loop. The "loop variable" is a variable of the same data type
as the members of the collection -- in this case, FormField.

Also, you need to recognize that not all FormField objects are
dropdowns, and trying to address the .DropDown property of a FormField
that is actually a checkbox or a textbox will cause an error. You have
to check the object's .Type property to make sure.

Another thing is that you can't be sure all the formfields are in the
first section of the document, so ActiveDocument.Sections(1) may not
be the right section to unprotect/reprotect. Since you're selecting
the formfield, you can use Selection.Sections(1) instead -- this
always grabs the section that contains the selection (since the field
can only be in one section).

Finally, with three or more possible values, it's more efficient to
replace the multiple If statements with a Select Case construction.

With all of that, you get a macro like this:

Sub ColorDropdowns()
Dim oFld As FormField
Dim oColor As WdColor
For Each oFld In ActiveDocument.FormFields
If oFld.Type = wdFieldFormDropDown Then
Select Case oFld.DropDown.Value
Case 1
oColor = wdColorRed
Case 2
oColor = wdColorBlue
Case 3
oColor = wdColorGreen
Case Else
oColor = wdColorAutomatic
End Select

oFld.Select
With Selection
.Sections(1).ProtectedForForms = False
.Font.Color = oColor
.Sections(1).ProtectedForForms = True
End With
End If
Next oFld
End Sub
 
J

Jean-Guy Marcil

Hi Greg,

Try this to get all dropdown fields and to go by section as in your example,
although it is not necessary, but hey, you are the boss!

'_______________________________________
Public Const ffRed As Variant = wdColorRed
Public Const ffBlue As Variant = wdColorBlue
Public Const ffGreen As Variant = wdColorGreen
'_______________________________________
Sub ChangeFontColor()

Dim Checkff As FormField
Dim MyDoc As Document
Dim ffRange As Range
Dim ffSec As Long
Dim ffValue As Variant
Dim ffDropName As String
Dim ffDropCol As Variant

Set MyDoc = ActiveDocument

For Each Checkff In MyDoc.FormFields

If Checkff.Type = wdFieldFormDropDown Then
Set ffRange = Checkff.Range
ffSec = ffRange.Sections.Item(1).Index
ffDropName = Checkff.Name
ffValue = Checkff.DropDown.Value

Select Case ffValue

Case 1
ffDropCol = ffRed

Case 2
ffDropCol = ffBlue

Case 3
ffDropCol = ffGreen

End Select

Change_Color ffDropName, ffSec, ffDropCol

End If

Next Checkff

Set MyDoc = Nothing
Set ffRange = Nothing

End Sub
'_______________________________________

'_______________________________________
Sub Change_Color(ffName As String, secIndex As Long, ffColor As Variant)

Dim ffDropRange As Range

With ActiveDocument
Set ffDropRange = .FormFields(ffName).Range
.Sections(secIndex).ProtectedForForms = False
ffDropRange.Font.Color = ffColor
.Sections(secIndex).ProtectedForForms = True
End With

Set ffDropRange = Nothing

End Sub
'_______________________________________

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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