#1
|
|||
|
|||
Change Text Colour/Box Shade if CheckBox is marked
Hi all! Thanks for welcoming me into this community. I am a basic VBA user (very beginner!) and hoping you may be able to help me out. I've been trying (embarassingly for hours) to create a VBA code that changes the text colour/check box colour/background shading to RED if the 'AWAITING RESPONSE' is checked, and to GREEN if the 'RESPONSE RECEIVED' is checked. I have attached a sample doc This is what I currently have: Private Sub Module1_ContentControlOnEnter(ByVal ContentControl As ContentControl, Cancel As Boolean) If (ContentControl.Title = "Awaiting Response" And ContentControl.Checked = True) Then ContentControl.Range.Font.ColorIndex = wdRed End If If (ContentControl.Title = "Awaiting Response" And ContentControl.Checked = False) Then ContentControl.Range.Font.ColorIndex = wdGreen End If End Sub Any help would be super appreciated!! |
#2
|
||||
|
||||
On Enter means the macro is running before you have made your selection. It would be more logical to run it after the CC value has been changed.
Based on the name of your macro, perhaps you are not aware that it only works when it has the right name AND the code is placed in the ThisDocument module. Try this code Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) If aCC.Title = "Awaiting Response" Then If aCC.Checked = True Then aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdRed Else aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdGreen End If End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Hi Andrew, holy moly yes it works perfectly. Just what I am hoping for! Thank you so much for revising my code for me and for the additional information regarding the requirement for same name and placement within the ThisDocument.
If I wanted to use that code a second time in the same macro (ie. to have second the "Response Received" checkbox change its text colour, can you advise me what I need to include in between the two codes to make that work? When I copy and paste the code back-to-back and only revise "Awaiting Response" to "Response Received", it prompts me with an error as followed: "Compile error: Ambiguous name detected: Document_ContentControlOnExit" Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) If aCC.Title = "Awaiting Response" Then If aCC.Checked = True Then aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdRed Else aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdGreen End If End If End Sub Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) If aCC.Title = "Response Received" Then If aCC.Checked = True Then aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdRed Else aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdGreen End If End If End Sub |
#4
|
||||
|
||||
It all has to get crammed into one macro - you can't have the same macro name in the same module.
Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) Select Case aCC.Title Case "Awaiting Response", "Response Received" If aCC.Checked = True Then aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdRed Else aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdGreen End If End Select End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Quote:
|
#6
|
|||
|
|||
Quote:
I have tried my hand at both a) combining the code (clearly I am making mistakes) as well as b) trying to create separate Modules with separate names. No luck on both fronts. Is there any suggestion as to how I can accomplish this please and thanks? I really love challenging myself with VBA, but boy is it demoralizing at times! Any assistance or links to resources would be very appreciated.! Ideally, I would love to be able to create separate codes that I can keep in their own separate modules, rather than combining the code into one macro (makes it much more difficult for me to try to write the code - less streamlined, more complicated for a beginner VBA-er IMO) Code 1: Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) Select Case aCC.Title Case "Awaiting Response", "Response Received" If aCC.Checked = True Then aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdRed Else aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdBlack End If End Select End Sub Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) With ContentControl.Range If ContentControl.Title = "Type" Then Select Case .Text Case "NCR" .Cells(1).Shading.BackgroundPatternColor = RGB(214, 227, 188) Case "CAR" .Cells(1).Shading.BackgroundPatternColor = RGB(182, 221, 232) Case "OFI" .Cells(1).Shading.BackgroundPatternColor = RGB(251, 212, 180) End Select End If End With End Sub |
#7
|
||||
|
||||
Quote:
In any event, your code is not sufficiently complex to require multiple Subs and trying to do so before you have a good grasp of the basics is expending effort in the wrong direction. Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) Select Case aCC.Title Case "Awaiting Response", "Response Received" If aCC.Checked = True Then aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdRed Else aCC.Range.Paragraphs(1).Range.Font.ColorIndex = wdBlack End If Case "Type" Select Case aCC.Range.Text Case "NCR" aCC.Range.Cells(1).Shading.BackgroundPatternColor = RGB(214, 227, 188) Case "CAR" aCC.Range.Cells(1).Shading.BackgroundPatternColor = RGB(182, 221, 232) Case "OFI" aCC.Range.Cells(1).Shading.BackgroundPatternColor = RGB(251, 212, 180) Case Else aCC.Range.Cells(1).Shading.BackgroundPatternColor = RGB(255, 255, 255) End Select End Select End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
Quote:
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Conditionally Colour/Shade Numeric Output Ranges | E.Buttimer | Mail Merge | 1 | 03-25-2021 02:20 PM |
Change text colour for content control labels? | Toe | Word | 1 | 01-17-2019 08:45 AM |
Change Text Colour in Cell Based on Text in Same Cell | PMC11 | Word VBA | 1 | 11-14-2017 09:15 PM |
Quickest way to change text to Arial size 11 specific colour | BlueClearSky | Word | 6 | 11-22-2013 03:34 PM |
How to change line height for marked text (in Word 2007)? ... as default for font? | pstein | Word | 1 | 01-14-2012 10:15 AM |