![]() |
|
#1
|
|||
|
|||
![]()
I have developed a word document that contains several pulldown menus created using the UI Content Control Boxes. I would like the font color be based on the item selected from the pulldown menu. How can this be done?
example: Select the gauge reading: 50 10 0 if 50 is selected, font color is green if 10 is selected, font color is yellow if 0 is selected, font color is red Thanks! |
#2
|
|||
|
|||
![]()
To change the font color when a value is chosen from a dropdown content control that has the title "Gauge reading", place this code in the ThisDocument module of the template this document is based on:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean) If CC.Title = "Gauge reading" And Not CC.ShowingPlaceholderText Then Select Case CC.Range.Text Case "50": CC.Range.Font.ColorIndex = wdGreen Case "10": CC.Range.Font.ColorIndex = wdYellow Case "0": CC.Range.Font.ColorIndex = wdRed End Select End If End Sub But the results may be hard to read, especially the yellow font color on a white background. You may be better off by changing the color of the background shading instead, with this version: Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean) If CC.Title = "Gauge reading" And Not CC.ShowingPlaceholderText Then Select Case CC.Range.Text Case "50": CC.Range.Shading.BackgroundPatternColor = wdColorGreen Case "10": CC.Range.Shading.BackgroundPatternColor = wdColorYellow Case "0": CC.Range.Shading.BackgroundPatternColor = wdColorRed End Select End If End Sub Either way, the change will take effect when the cursor exits from the control, and not while the value is being selected. Two things to note: First, this will work properly only with a dropdown content control, not with a combo box content control which looks similar. In a combo box, the user can type any text, so none of the three cases in the code would execute. Second, to make the code correspond to the specific content control, the Title value in the control's Properties dialog must be the same (including capitalization) as the text in quotes in the If statement. If you have multiple content controls, each one must have its own If and Case statements within the single macro. |
#3
|
||||
|
||||
![]()
@jjfreedman: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
See also: https://www.msofficeforums.com/word-...html#post47254
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
Thanks jjfreedman and macropod! This is my first attempt at using VB and it seems to work!
Thanks! |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Lock or unlock a content control depending on the selection from the drop down list | ncruz | Word VBA | 4 | 10-15-2021 08:50 AM |
![]() |
jsc_msoffice | Word VBA | 2 | 05-21-2021 09:51 PM |
Content control font color | rkferguson | Word VBA | 1 | 12-18-2018 05:06 AM |
![]() |
gennatr12 | Word VBA | 7 | 03-26-2017 08:51 PM |
Combo Box Content Control Calculate on selection help | ciresuark | Word | 0 | 03-06-2015 01:49 PM |