PDA

View Full Version : [SOLVED:] Conditional Formatting based on Content Control



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

macropod
02-20-2017, 01:41 PM
Instead of (or in addition to) using:
.Range.Cells(1).Shading.BackgroundPatternColorIndex
you could use:
.Range.Cells(1).Range.Previous.Cells(1).Shading.BackgroundPatternColorIndex

Note: Thread split from http://www.vbaexpress.com/forum/showthread.php?51368-Conditional-Format-of-Word-Content-Control-Drop-down-box&p=318567#post318567

Fossie
02-20-2017, 03:13 PM
Many thanks for your reply - especially the speed of it!

Am I being dim? I've used .Range.Cells(1).Range.Previous.Cells(1).Shading.BackgroundPatternColorIndex = and it's changing the previous cell's colour (again, thank you - I would never have found that), but not the current cell's as well?
I've tried to split the two instructions as an alternative, but can't get it to change both of the cells to the correct state.

Much obliged,
Fossie

macropod
02-20-2017, 04:36 PM
If you want to colour both cells, you need to use both statements...

Fossie
02-21-2017, 01:38 AM
Thanks once again Paul. I was being dim - I thought the line you quoted would do both!
Working a treat now and I am very grateful.

F.