PDA

View Full Version : Highlight Blanks In A Table & Grey Out Table Cells Given "Yes" in Content Control



TimJones
08-30-2018, 11:06 AM
I would like to highlight cells yellow when they are blank. Once a user fills them out, the yellow highlight is removed.

Secondly, I would like to grey out certain fields based on whether a content control field is answered "Yes" or "No".

22803

gmaxey
08-30-2018, 11:35 AM
Put this in the ThisDocument module:


Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If Not ContentControl.ShowingPlaceholderText Then
ContentControl.Range.Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
Else
ContentControl.Range.Cells(1).Shading.BackgroundPatternColor = wdColorYellow
End If
Select Case ContentControl.Title
Case "YesNo1"
If ContentControl.Range.Text = "Yes" Then
'Do this
Else
'Do that
End If
End Select
End Sub

Tag you YesNo CC "YesNo1" "YesNo2" etc.

What do you mean "grey out fields"

TimJones
08-30-2018, 12:35 PM
Thank you, Greg.

By "grey out", I just mean a grey background in a table cell. Also, will the yellow highlight work in a table without a content control? It's just a basic table and I would like the empty/blank cells to be highlighted. Picture below might better explain.

22802

gmaxey
08-30-2018, 01:26 PM
Tim,

You could run a procedure manually to highlight all empty cells in a table (or selective cells), but there is no event triggers from just filling in a table cell.


Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
On Error Resume Next

Select Case ContentControl.Title
Case "YesNo1"
If ContentControl.Range.Text = "Yes" Then
ActiveDocument.Tables(1).Cell(2, 2).Range.Shading.BackgroundPatternColor = wdColorGray50
Else
ActiveDocument.Tables(1).Cell(2, 2).Range.Shading.BackgroundPatternColor = wdColorAutomatic
End If
End Select
End Sub

Sub ShadedCells()
Dim oCell As Cell
For Each oCell In ActiveDocument.Tables(1).Range.Cells
If Len(oCell.Range.Text) = 2 Then
oCell.Range.Shading.BackgroundPatternColor = wdColorYellow
Else
oCell.Range.Shading.BackgroundPatternColor = wdColorAutomatic
End If
Next oCell
End Sub

TimJones
09-11-2018, 08:59 AM
Thank you, Greg. Got the greying out to work.

Can the greying out be locked (to prevent entering anything) if I use incorporate Legacy Form fields into the table?