Hi Andrew,
I've made the changes to the tags that you suggested and the code you provided works perfectly! Thank you.
Edit: After a little bit more trial-and-error, I managed to write a simple FOR loop that greys out a section, so help with the question below is no longer needed!
I would also like the whole table to change colours if the top checkbox is checked (i.e., "check this box if this section does not apply").
I believe to do that I would need to move that checkbox to a new row at the top of the table (instead of being text outside the table as it currently is), so I have done that and given that checkbox a unique tag called 'MasterNA'.
I've tried adapting the code you provided to grey out the whole table based on this 'MasterNA' checkbox. I haven't figured out how to do that yet, but I have figured out a couple of dozens ways to not do that 😅
Is it possible to adapt this code to change the colour of the whole table based on the 'MasterNA' checkbox? Or would I need to put in something more complicated, like maybe a FOR loop that would cycle through each row in the table?
Here is the code I used to get a "master" checkbox to grey out a table. The attached file contains the same code but with additional inline comments.
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim lngColour As Long, i As Long, aCC As ContentControl
If CCtrl.Type = wdContentControlCheckBox Then 'section 1 - MASTER checkbox disables or re-enables a section
Select Case CCtrl.Tag
Case "MASTER"
lngColour = wdGray50
If CCtrl.Checked Then
For Each aCC In CCtrl.Range.Tables(1).Range.ContentControls
aCC.LockContents = False
aCC.Range.Font.ColorIndex = lngColour
aCC.Range.Font.StrikeThrough = True
If aCC.Type = wdContentControlCheckBox Then
If aCC.Tag <> "MASTER" Then
aCC.Checked = False
End If
End If
aCC.LockContents = True
If aCC.Tag = "MASTER" Then
aCC.LockContents = False
aCC.Range.Font.StrikeThrough = False
End If
Next
End If 'end second IF (If.CCtrl.Checked Then)
If CCtrl.Checked = False Then
For Each aCC In CCtrl.Range.Tables(1).Range.ContentControls
aCC.LockContents = False
aCC.Range.Font.ColorIndex = wdAuto
aCC.Range.Font.StrikeThrough = False
If aCC.Type = wdContentControlRichText Then
aCC.LockContents = True
End If
Next
End If
End Select
End If
If CCtrl.Type = wdContentControlCheckBox Then 'section 2 - colour-code rows based on Compliance status Yes/No/NA
If CCtrl.Checked Then
For Each aCC In CCtrl.Range.Cells(1).Range.ContentControls
If aCC.Tag <> CCtrl.Tag Then aCC.Checked = False
Next aCC
Select Case CCtrl.Tag
Case "Yes"
lngColour = wdGreen
Case "No"
lngColour = wdRed
Case "NA"
lngColour = wdDarkYellow
' Case "MASTER"
' lngColour = wdGray50
' Case Else
' lngColour = wdAuto
End Select
CCtrl.Range.Font.ColorIndex = lngColour
Set aCC = CCtrl.Range.Rows(1).Range.ContentControls(1)
aCC.LockContents = False
aCC.Range.Font.ColorIndex = lngColour
aCC.LockContents = True
End If
End If
End Sub