Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2023, 10:36 AM
PbFoot PbFoot is offline font color based on content control pulldown selection Windows 10 font color based on content control pulldown selection Office 2019
Novice
font color based on content control pulldown selection
 
Join Date: Jan 2023
Posts: 2
PbFoot is on a distinguished road
Question font color based on content control pulldown selection

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!
Reply With Quote
  #2  
Old 01-02-2023, 03:03 PM
jjfreedman jjfreedman is offline font color based on content control pulldown selection Windows 10 font color based on content control pulldown selection Office 2019
Advanced Beginner
 
Join Date: May 2012
Location: https://jay-freedman.info
Posts: 39
jjfreedman is on a distinguished road
Default font color based on content control pulldown selection

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.
Reply With Quote
  #3  
Old 01-02-2023, 07:29 PM
macropod's Avatar
macropod macropod is offline font color based on content control pulldown selection Windows 10 font color based on content control pulldown selection Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

@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]
Reply With Quote
  #4  
Old 01-03-2023, 08:22 AM
PbFoot PbFoot is offline font color based on content control pulldown selection Windows 10 font color based on content control pulldown selection Office 2019
Novice
font color based on content control pulldown selection
 
Join Date: Jan 2023
Posts: 2
PbFoot is on a distinguished road
Default

Thanks jjfreedman and macropod! This is my first attempt at using VB and it seems to work!
Thanks!
Reply With Quote
Reply



Similar Threads
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
font color based on content control pulldown selection Change Value of a Content Control based on the value of another content Control 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
font color based on content control pulldown selection Edit table cells based on content control selection 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:57 AM.


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