Fossie
02-20-2017, 11:27 AM
It's not clear whether you want to colour the: (a) content control's background; (b) font; or, perhaps (c) table cell that the content control is in.
For a:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "Risk" Then
Select Case .Range.Text
Case "High": .Range.Font.ColorIndex = wdRed
Case "Medium": .Range.Font.ColorIndex = wdYellow
Case "Low": .Range.Font.ColorIndex = wdBrightGreen
Case Else: .Range.Font.ColorIndex = wdAuto
End Select
End If
End With
End Sub
For b:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "Risk" Then
Select Case .Range.Text
Case "High": .Range.Shading.BackgroundPatternColorIndex = wdRed
Case "Medium": .Range.Shading.BackgroundPatternColorIndex = wdYellow
Case "Low": .Range.Shading.BackgroundPatternColorIndex = wdBrightGreen
Case Else: .Range.Shading.BackgroundPatternColorIndex = wdNoHighlight
End Select
End If
End With
End Sub
For c:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "Risk" Then
Select Case .Range.Text
Case "High": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdRed
Case "Medium": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdYellow
Case "Low": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdBrightGreen
Case Else: .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdNoHighlight
End Select
End If
End With
End Sub
Whichever version you want to use should be saved to the 'ThisDocument' code module of the document or it's template. The file will need to be saved as a macro-enabled file (dotm template or docm document). It's also possible to combine changes in font colour & background shading.
Thread resurrection!
This has also worked a treat for me, but I'd also like it to change the background colour of the cell immediately to the left of the control as well as the control cell itself. I've tried various bits of code but am struggling with the range.
Many thanks in advance,
Fossie
For a:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "Risk" Then
Select Case .Range.Text
Case "High": .Range.Font.ColorIndex = wdRed
Case "Medium": .Range.Font.ColorIndex = wdYellow
Case "Low": .Range.Font.ColorIndex = wdBrightGreen
Case Else: .Range.Font.ColorIndex = wdAuto
End Select
End If
End With
End Sub
For b:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "Risk" Then
Select Case .Range.Text
Case "High": .Range.Shading.BackgroundPatternColorIndex = wdRed
Case "Medium": .Range.Shading.BackgroundPatternColorIndex = wdYellow
Case "Low": .Range.Shading.BackgroundPatternColorIndex = wdBrightGreen
Case Else: .Range.Shading.BackgroundPatternColorIndex = wdNoHighlight
End Select
End If
End With
End Sub
For c:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "Risk" Then
Select Case .Range.Text
Case "High": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdRed
Case "Medium": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdYellow
Case "Low": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdBrightGreen
Case Else: .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdNoHighlight
End Select
End If
End With
End Sub
Whichever version you want to use should be saved to the 'ThisDocument' code module of the document or it's template. The file will need to be saved as a macro-enabled file (dotm template or docm document). It's also possible to combine changes in font colour & background shading.
Thread resurrection!
This has also worked a treat for me, but I'd also like it to change the background colour of the cell immediately to the left of the control as well as the control cell itself. I've tried various bits of code but am struggling with the range.
Many thanks in advance,
Fossie