Search code examples
checkboxms-wordtextcolor

MS Word. Changing the text color depending on checked/unchecked checkbox


I'm using MS Word 2016.

Is it possible to implement changing the text color from black to red depending on checked/unchecked checkbox?


Solution

  • The following macro assumes you're using a Checkbox Content Control titled 'Checkbox1' and, for the text to be re-formatted, a Rich Text Content Control titled 'ColorText':

    Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
    Dim Clr As Long
    With CCtrl
      Select Case .Title
        Case "Checkbox1"
            Select Case .Checked
              Case True: Clr = wdRed
              Case Else: Clr = wdAuto
            End Select
          ActiveDocument.SelectContentControlsByTitle("ColorText")(1).Range.Font.ColorIndex = Clr
      End Select
    End With
    End Sub
    

    The code goes in the 'ThisDocument' code module of the document or its template.