Can I use VBA in PowerPoint to change the text color and fill color of a TextBox depending on the text it contains?

Please help me use VBA in PowerPoint to conditionally change the text color and fill color of a TextBox depending on the text it contains.  I would like it to apply to all slides in the presentation, but only to TextBox objects named either tbPrevious or  tbCurrent.  I’ve attached a presentation to provide an example.


If the value in tbPrevious =
“R” then Fill = Hex: #FF0000
“Y” then Fill = Hex: #FFFF00
“G” then Fill = Hex: #00FF00
“B” then Fill = Hex: #0000FF   and   Text Color = Hex: #FFFFFF
Else Fill = Hex: #FFFFFF   and   Text Color = Hex: #000000


Here are the RGB values in case that is easier:


If the value in tbCurrent =
“R” then Fill = R: 255 G: 0 B: 0
“Y” then Fill = R: 255, G: 255, B: 0
“G” then Fill = R: 0, G: 255, B: 0
“B” then Fill = R: 0, G: 0, B: 255   and   Text Color = R: 255, G: 255, B: 255
Else Fill = R: 255, G: 255, B: 255   and   Text Color = R: 0, G: 0, B: 0


My coworker has a presentation with 276 slides in in that she updates every week, and around 100 of the slides have these two text boxes in them; so, this will save her a lot of time.

We are both using PowerPoint 2013, but the other guy that helps update the presentation is still using PowerPoint 2007.

Thanks in advance for your help,
   Jon
ConditionalFormatting.pptx
Jon BredensteinerProject ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jamie GarrochPowerPoint Consultant & DeveloperCommented:
You can use something like this:

' Free PowerPoint Macro provided by youpresent.co.uk
' Under Creative Commons License
Option Explicit

Public Sub RecolourTextBox()
  Dim oSld As Slide
  Dim oShp As Shape
  
  For Each oSld In ActivePresentation.Slides
    For Each oShp In oSld.Shapes
      With oShp
        If .Type = msoTextBox Then
          If .HasTextFrame Then
            Select Case UCase(oShp.TextFrame.TextRange.Text)
              Case "R": .Fill.ForeColor.RGB = RGB(255, 0, 0)
              Case "Y": .Fill.ForeColor.RGB = RGB(255, 255, 0)
              Case "G": .Fill.ForeColor.RGB = RGB(0, 255, 0)
              Case "B": .Fill.ForeColor.RGB = RGB(0, 0, 255): .TextFrame.TextRange.Font.Color = RGB(255, 255, 255)
              Case Else: .Fill.ForeColor.RGB = RGB(255, 255, 255): .TextFrame.TextRange.Font.Color = RGB(0, 0, 0)
            End Select
          End If
        End If
      End With
    Next
  Next
End Sub

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jon BredensteinerProject ManagerAuthor Commented:
Thank you Jamie for your quick reply, and the great solution!


Just one more quick thing...  Is there a way to only apply this code to msoTextBox objects that are named either "tbPrevious" or "tbCurrent"?

Thanks again for your help,
   Jon
0
Jamie GarrochPowerPoint Consultant & DeveloperCommented:
No probs Jon.

Change this line:
If .HasTextFrame Then

Open in new window

to this:
If .HasTextFrame And (.Name="tbPrevious" Or .Name="tbCurrent") Then

Open in new window

1
Jon BredensteinerProject ManagerAuthor Commented:
Perfect, thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft PowerPoint

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.