Search code examples
vbapowerpoint

How to search for multiple strings and change font color


I need to search for multiple strings in slide text boxes and change the color of the paragraph. For example if the paragraph starts with "A." or "1." or "Z." -- then I want to turn the paragraph bold.

I can setup individual searches ---but is there a way to search for all three criteria at the same time?

Here are the individual searches:

 For Each curShape In curSlide.Shapes
      If curShape.TextFrame.HasText Then
            Set curText = curShape.TextFrame.TextRange
              
              With curText
                For iPara = .Paragraphs.Count To 1 Step -1
                  If Left(.Paragraphs(iPara), 2) = "A. " Then
                      .Paragraphs(iPara).Font.Bold = True
                  End If

                For iPara = .Paragraphs.Count To 1 Step -1
                  If Left(.Paragraphs(iPara), 2) = "1. " Then
                      .Paragraphs(iPara).Font.Bold = True
                  End If

               Next
              End With

Solution

  • You don't want to create additional code for each criteria, use a Case statement

     For Each curShape In curSlide.Shapes
          If curShape.TextFrame.HasText Then
                Set curText = curShape.TextFrame.TextRange
    
                  With curText
                    For iPara = .Paragraphs.Count To 1 Step -1
                      Select Case Left(.Paragraphs(iPara), 2)
                          Case "A.", "1."   '# Add additional cases separated by commas
                               .Paragraphs(iPara).Font.Bold = True
                          Case Else
                              'do nothing
                      End Select
                   Next
                  End With