Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-24-2021, 01:26 AM
TheAnimal TheAnimal is offline Apply 2 VBA codes to a single content control drop down Windows XP Apply 2 VBA codes to a single content control drop down Office 2019
Novice
Apply 2 VBA codes to a single content control drop down
 
Join Date: Sep 2021
Posts: 4
TheAnimal is on a distinguished road
Default Apply 2 VBA codes to a single content control drop down

Hi, complete noob here
Please can I get some advice with applying two VBA codes to a single content control drop down box.
I've managed to get them to work individually but not sure how to apply both simultaneously.
I have a drop down with 3 options: Red, Amber or Green, which turn the corresponding colour on exit. I also want the value to populate as opposed to the display name.
A huge thank you to Greg Maxey and Paul Edstein who's website and previous answers to threads have got me this far!

Code currently as follows:

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim lngIndex As Long
Dim strValue As String
Select Case CC.Title
Case "RAG"
If CC.ShowingPlaceholderText Then Exit Sub
With CC
If .Title = "RAG" Then
Select Case .Range.Text
Case "RED": .Range.Font.Color = RGB(178, 34, 34)
Case "AMBER": .Range.Font.Color = RGB(255, 165, 0)
Case "GREEN": .Range.Font.Color = RGB(50, 205, 50)


Case Else: .Range.Font.ColorIndex = wdAuto
For lngIndex = 2 To .DropdownListEntries.Count
If .DropdownListEntries(lngIndex).Text = .Range.Text Then
strValue = .DropdownListEntries(lngIndex).Value
.Type = wdContentControlText
.Range.Text = strValue
.Type = wdContentControlDropdownList
Exit For
End If
Next lngIndex
End With
Case Else
End Select
lbl_Exit:
Exit Sub
End Sub

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "RAG" Then
Select Case .Range.Text
Case "RED": .Range.Font.Color = RGB(178, 34, 34)
Case "AMBER": .Range.Font.Color = RGB(255, 165, 0)
Case "GREEN": .Range.Font.Color = RGB(50, 205, 50)
Case Else: .Range.Font.ColorIndex = wdAuto
End Select

How can I combine these to perform both instructions??

Thank you and please be aware that I already know how incompetent I am with VBA
Reply With Quote
  #2  
Old 09-24-2021, 04:30 AM
gmayor's Avatar
gmayor gmayor is offline Apply 2 VBA codes to a single content control drop down Windows 10 Apply 2 VBA codes to a single content control drop down Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following will do what you require. The second macro is superfluous.The macro assumes that the value associated with the list item is not the same as the list item e.g.


Choose an item.
RED|Value 1
AMBER|Value 2
GREEN|Value 3


Thus if you select RED, 'Value 1' is displayed.


You can edit the list easily with
https://www.gmayor.com/insert_content_control_addin.htm
Code:
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim lngIndex As Long
Dim strValue As String
    Select Case CC.Title
        Case "RAG"
            With CC
                If .ShowingPlaceholderText Then Exit Sub
                Select Case .Range.Text
                    Case "RED": .Range.Font.Color = RGB(178, 34, 34)
                    Case "AMBER": .Range.Font.Color = RGB(255, 165, 0)
                    Case "GREEN": .Range.Font.Color = RGB(50, 205, 50)
                    Case Else: .Range.Font.ColorIndex = wdAuto
                End Select
                For lngIndex = 2 To .DropdownListEntries.Count
                    If .DropdownListEntries(lngIndex).Text = .Range.Text Then
                        strValue = .DropdownListEntries(lngIndex).Value
                        .Type = wdContentControlText
                        .Range.Text = strValue
                        .Type = wdContentControlDropdownList
                        Exit For
                    End If
                Next lngIndex
            End With
        Case Else
    End Select
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-24-2021, 04:42 AM
TheAnimal TheAnimal is offline Apply 2 VBA codes to a single content control drop down Windows XP Apply 2 VBA codes to a single content control drop down Office 2019
Novice
Apply 2 VBA codes to a single content control drop down
 
Join Date: Sep 2021
Posts: 4
TheAnimal is on a distinguished road
Default

Graham, You are a genius! It does everything I wanted!
Thank you so so much
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apply 2 VBA codes to a single content control drop down Drop Down & Text Content Control forskare Word VBA 3 06-13-2019 08:21 PM
Apply 2 VBA codes to a single content control drop down Assigning Macro to Drop Down list Content Control aussiew Word VBA 5 03-10-2019 02:55 PM
Table with content control drop down - change font DebA Word 3 04-29-2018 09:07 PM
Apply 2 VBA codes to a single content control drop down How to get a Drop Down List Content Control box to fill in other areas snips1982 Word 2 03-22-2017 03:37 AM
Word drop-down content control dependent display issue vvcat Word 6 02-03-2015 11:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:52 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft