Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2024, 02:51 PM
mj_sklarWRK mj_sklarWRK is offline Change Text Colour if CheckBox is marked Windows 11 Change Text Colour if CheckBox is marked Office 2021
Novice
Change Text Colour if CheckBox is marked
 
Join Date: Feb 2024
Posts: 2
mj_sklarWRK is on a distinguished road
Default 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

Attached Files
File Type: docm checkboxes autoformat draft.docm (27.6 KB, 5 views)
Reply With Quote
  #2  
Old 02-11-2024, 06:38 PM
Guessed's Avatar
Guessed Guessed is offline Change Text Colour if CheckBox is marked Windows 10 Change Text Colour if CheckBox is marked Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 02-14-2024, 11:01 AM
mj_sklarWRK mj_sklarWRK is offline Change Text Colour if CheckBox is marked Windows 11 Change Text Colour if CheckBox is marked Office 2021
Novice
Change Text Colour if CheckBox is marked
 
Join Date: Feb 2024
Posts: 2
mj_sklarWRK is on a distinguished road
Default

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
Attached Files
File Type: docm checkboxes autoformat draft.docm (35.4 KB, 3 views)

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.
Reply With Quote
Reply

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 Text Colour if CheckBox is marked 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft