Consulting

Results 1 to 9 of 9

Thread: Conditional Format of Word Content Control Drop down box

  1. #1

    Conditional Format of Word Content Control Drop down box

    I am creating a Word form in Word 2010. I have created a drop down box to indicate the status of a project. There are three options, Red, Yellow, And Green. I would like to format the drop down box so that its background colour will correspond with the choice.
    Thank you in advance.
    Scotto

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It's not clear whether you want to colour the: (a) content control's background; (b) font; or, perhaps (c) table cell that the content control is in.

    For a:
    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    With ContentControl
      If .Title = "Risk" Then
        Select Case .Range.Text
          Case "High": .Range.Font.ColorIndex = wdRed
          Case "Medium": .Range.Font.ColorIndex = wdYellow
          Case "Low": .Range.Font.ColorIndex = wdBrightGreen
          Case Else: .Range.Font.ColorIndex = wdAuto
        End Select
      End If
    End With
    End Sub
    For b:
    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    With ContentControl
      If .Title = "Risk" Then
        Select Case .Range.Text
          Case "High": .Range.Shading.BackgroundPatternColorIndex = wdRed
          Case "Medium": .Range.Shading.BackgroundPatternColorIndex = wdYellow
          Case "Low": .Range.Shading.BackgroundPatternColorIndex = wdBrightGreen
          Case Else: .Range.Shading.BackgroundPatternColorIndex = wdNoHighlight
        End Select
      End If
    End With
    End Sub
    For c:
    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    With ContentControl
       If .Title = "Risk" Then
        Select Case .Range.Text
          Case "High": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdRed
          Case "Medium": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdYellow
          Case "Low": .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdBrightGreen
          Case Else: .Range.Cells(1).Shading.BackgroundPatternColorIndex = wdNoHighlight
        End Select
        End If
    End With
    End Sub
    Whichever version you want to use should be saved to the 'ThisDocument' code module of the document or it's template. The file will need to be saved as a macro-enabled file (dotm template or docm document). It's also possible to combine changes in font colour & background shading.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3

    It works!

    I have just tried your code and it worked very well. Thank you very much for the speedy reponse!
    Scotto

  4. #4
    VBAX Newbie
    Joined
    Mar 2019
    Posts
    1
    Location

    White background w/colour font change only on selection

    I tried all 3of these codes to try and adapt to my needs. I failed. Can you help me to change the font color only when a drop down is selected? I have 2 selections in my drop down, "yes" is a red Font and no is black. The above codes change the cell colors not just the font. Thank you very much!
    Last edited by macropod; 03-22-2019 at 02:32 PM. Reason: Deleted unnecessary quote of entire post replied to

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Lin View Post
    I tried all 3of these codes …
    The above codes change the cell colors not just the font.
    That simply isn't so. The:
    • first colours only the text;
    • second colours only the content control background;
    • third colours the content control's cell.
    None of the codes changes both the font colour and the background colour.
    Last edited by macropod; 10-07-2021 at 03:52 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6

    Arrrrggghhhh Shouting slightly quieter


    Hi macropod, I know this is an old and well worn topic but am trying to add coloured cell backgrounds to a drop down list dependant on choice selection in Word using office 365 an i have tried all of your coding below to try and get some change whether its cell colour, font or anything but nothing changes please help. I'm sure it's down to Muppetry on my part but it's doing my head in.... Please help
    Cheers
    Matt
    Last edited by macropod; 10-07-2021 at 03:53 PM.

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For what you've described, you need the third version of the code, which needs to be placed in the 'ThisDocument' code module of either the document or its template.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Hi again macropod, Thanks but I've tried all 3 versions and placed it in the "ThisDocument" module and get nothing hence Does it make any difference which version of windows/office(word) in use?
    Im using Win 10 and Office 365.
    Thanks
    Matt

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The code works on all versions of Office from 2007 onwards, on any supported Windows or Mac platform.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •