![]() |
|
#1
|
|||
|
|||
![]()
Good evening,
I am trying to create/find a macro for MS Word VBA to adjust the font colour of the various lists (I believe there are 14 small lists in the document) based on what is selected. For the 14 items, the default value includes one of the following (if they have not chosen one of the other options in the list): Choose an Item If "Yes", Select Select Date n/a What I want to do is to make those values in the font colour red. Any other option they choose in the various lists should show up as black, to show that they have filled in the field. A macro to do all of this would be greatly appreciated. Do let me know if you need any other information. |
#2
|
||||
|
||||
![]()
What kind of dropdown list (content control, formfield, ActiveX) and what do you want to colour - the dropdown item's colour or something else?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Good evening Paul,
Thanks for the response. The list was created with content control. I want to colour the font of the text. So for the four instances I listed, those I would ideally want in red. For everything else I want it in black. Aesthetically, the red looks perfect on what I am doing. But having it stay red almost looks like it is incomplete, so having the colour change to black once the user selects one of the other dropdown menu options is a definite advantage. Let me know if you need to know anything else, Dave |
#4
|
||||
|
||||
![]()
In that case, you could add a 'ContentControlOnExit' macro to your document's 'ThisDocument' code module, coded along the lines of:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) With CCtrl Select Case .Title Case "Dropdown1", "Dropdown2" Select Case .Range.Text Case .PlaceholderText, "If ""Yes"", Select", "n/a" .Range.Font.ColorIndex = wdRed Case Else .Range.Font.ColorIndex = wdAuto End Select End Select End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Hi Paul,
Two things for you: 1) When I add "Select Date" or another one of the headers, I get an error message. Not sure why. 2) All of the dropdown lists turn into red font, as opposed to just those options. Would you know why? Thanks! |
#6
|
||||
|
||||
![]()
Without seeing your document, or more detail on what you've done, I can't say why you'd get those results.
The attached document has two rows of content controls - dropdown, date & text. Each displays in red while the default text is displayed, the dropdown also does so if 'N/A' is selected; otherwise they display normally.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
It's working a lot better now! Almost everything works.
One last problem: I have the same list name for the two date fields as the other fields (titled "Select"), with the default selection saying "Select Date". I want that to be red until the user selects a date, and then it will go black. But I get Run-time Error 91: "Object variable or With block variable not set." What do you think I need to change, especially because everything else works. |
#8
|
||||
|
||||
![]()
As I said before:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]()
Sorry, keeps timing out when I try to attach. See attached now.
I took the code out of this so you can just try it from scratch. If the code you put in works, I'll just copy and paste it into the other one and see if it works. Really appreciate it! |
#10
|
||||
|
||||
![]()
It's not much good posting the document without the code, as that's what's generating the error messages.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
No problem. Here you are.
|
#12
|
||||
|
||||
![]()
The problem was that you'd deleted the placeholder text for the two date controls. The following macro will fix that:
Code:
Sub FixDateCtrls() With ActiveDocument.SelectContentControlsByTitle("Select Date") .Item(1).SetPlaceholderText Text:="Select Date" .Item(2).SetPlaceholderText Text:="Select Date" End With End Sub Try the following: Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) Application.ScreenUpdating = False With CCtrl Select Case .Title Case "Select" Select Case .Range.Text Case .PlaceholderText, "n/a", "If Yes, Select" .Range.Font.ColorIndex = wdRed Case Else .Range.Font.ColorIndex = wdAuto End Select Case "Select Date" Select Case .Range.Text Case .PlaceholderText .Range.Font.ColorIndex = wdRed Case Else .Range.Font.ColorIndex = wdAuto End Select With .Range.Font .Size = 8 .Italic = True End With Case Else End Select End With Application.ScreenUpdating = True End Sub You might also want to set each content control's 'cannot be deleted' property, to guard against errant users.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
![]()
Hey Paul,
Yeah I left those title selections in there and just included the ones I needed to the right. Anytime I run it now, I get an error when selecting a date (same run-time error as before) and it highlights the "Case .PlaceholderText". |
#14
|
||||
|
||||
![]()
Did you run the FixDateCtrls macro first?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
![]()
Sorry had to close it and now it works.
![]() Does that mean that the macro will have to be run before each time it is used? |
![]() |
Tags |
color, conditional formatting, list |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
pgammag | Word | 9 | 08-20-2019 04:17 PM |
Dropdown conditional formatting | tlkc67 | Word VBA | 6 | 10-05-2014 10:54 AM |
![]() |
info_guy2 | Excel | 1 | 07-03-2014 10:07 AM |
Dropdown List in Microsoft Word 2010 | bfarquhar | Word | 2 | 04-02-2014 07:48 PM |
![]() |
stijnvanhoof | Mail Merge | 1 | 11-13-2012 01:55 PM |