#1
|
|||
|
|||
Change Text Colour if CheckBox is marked
I'm using Word 2021 to create a checklist form that contains several dozen questions, each with three checkboxes: yes, no, N/A. The checkboxes are all named using titles along the lines of "ChkYes_1", "ChkYes_2", "ChkNo_2" etc. I would like the questions and checkboxes to change colour based on the responses (e.g. turn green if "yes" or red if "no"). I've figured out how to do this for one row of checkboxes, calling the checkboxes by title in the code. Is there a way to adapt this code to apply to all the questions, without needing to duplicate this section of code for each unique title? Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) Dim VarCLR As Long Dim i As Long With CCtrl Select Case .Title Case "Chk1BNo_1" Select Case .Checked Case True: VarCLR = wdRed Case Else: VarCLR = wdAuto End Select ActiveDocument.SelectContentControlsByTitle("Chk1BNo_1")(1).Range.Font.ColorIndex = VarCLR ActiveDocument.SelectContentControlsByTitle("Text1B_1")(1).Range.Font.ColorIndex = VarCLR End Select End With End Sub |
#2
|
||||
|
||||
Based on your sample doc, make changes to each of the Tag properties on the checkboxes so the Tag matches the text on the checkbox. This means each checkbox tag could be Yes, No or NA. Then try this text which will also ensure that you can only check one box at a time.
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 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 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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
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").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 Last edited by mj_sklarWRK; 02-14-2024 at 01:28 PM. Reason: I found the solution to my question; updating post with the answer. |
Tags |
checkboxes, content control |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
CHange colour of text box if has a tick or a X | chris.james | Word VBA | 1 | 10-01-2023 12:50 AM |
Change "fill" text colour to no colour | darkmaster006 | Word VBA | 15 | 08-22-2023 10:10 AM |
Change Text Colour/Box Shade if CheckBox is marked | cavals07 | Word VBA | 7 | 01-30-2023 11:05 AM |
Change text colour for content control labels? | Toe | Word | 1 | 01-17-2019 08:45 AM |
How to change line height for marked text (in Word 2007)? ... as default for font? | pstein | Word | 1 | 01-14-2012 10:15 AM |