font color based on content control pulldown selection
To change the font color when a value is chosen from a dropdown content control that has the title "Gauge reading", place this code in the ThisDocument module of the template this document is based on:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
If CC.Title = "Gauge reading" And Not CC.ShowingPlaceholderText Then
Select Case CC.Range.Text
Case "50": CC.Range.Font.ColorIndex = wdGreen
Case "10": CC.Range.Font.ColorIndex = wdYellow
Case "0": CC.Range.Font.ColorIndex = wdRed
End Select
End If
End Sub
But the results may be hard to read, especially the yellow font color on a white background. You may be better off by changing the color of the background shading instead, with this version:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
If CC.Title = "Gauge reading" And Not CC.ShowingPlaceholderText Then
Select Case CC.Range.Text
Case "50": CC.Range.Shading.BackgroundPatternColor = wdColorGreen
Case "10": CC.Range.Shading.BackgroundPatternColor = wdColorYellow
Case "0": CC.Range.Shading.BackgroundPatternColor = wdColorRed
End Select
End If
End Sub
Either way, the change will take effect when the cursor exits from the control, and not while the value is being selected.
Two things to note:
First, this will work properly only with a dropdown content control, not with a combo box content control which looks similar. In a combo box, the user can type any text, so none of the three cases in the code would execute.
Second, to make the code correspond to the specific content control, the Title value in the control's Properties dialog must be the same (including capitalization) as the text in quotes in the If statement. If you have multiple content controls, each one must have its own If and Case statements within the single macro.
|