Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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, 5 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
 

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 10:05 PM.


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