Share via

VBA PowerPoint problem

Anonymous
2019-10-30T10:19:28+00:00

Hi,

I'm trying to do a search of about 600 PowerPoint slides where I have the wording Jones v Brown for example and replace the "v" with "!!" and then bold and highlight text.  So it should look like:

Jones !! Brown

I can do the search and change the "v" to "!!" but I cannot work out how to bold (or highlight) the "!!".  And when I insert some more code to bold (I've not included this code below) it bolds the entire slide instead of the "!!".  Any ideas on what I'm missing or doing wrong?  Many thanks in advance.

Sub regex()

Dim regX As Object

Dim osld As Slide

Dim oshp As Shape

Dim strInput As String

Dim strInput2 As String                  ' Extra variable - not required

Dim b_found As Boolean

Set regX = CreateObject("vbscript.regexp")

With regX

.Global = True

'Note + is a special character so +

.Pattern = " v "

End With

For Each osld In ActivePresentation.Slides

For Each oshp In osld.Shapes

If oshp.HasTextFrame Then

If oshp.TextFrame.HasText Then

strInput = oshp.TextFrame.TextRange.Text

b_found = regX.Test(strInput)

If b_found = True Then

strInput = regX.Replace(strInput, " !! ")

oshp.TextFrame.TextRange.Text = strInput  'This puts the (changed) input of strInput into the text    

'But I cannot work out how to select just the ' !!! ' and then bold and highlight it (or change the font size, etc for that matter of just that text)

End If

End If

End If

Next oshp

Next osld

End Sub

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2019-10-30T17:59:09+00:00

    Thank you so much John.  I'm new to VBA, so this has been very difficult for me to work out.  The code you've suggested works perfectly.  That said, was I close in my original code, or did I really need to use a different approach like you've done.

    Kind regards,

    Dominic

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2019-10-30T13:52:37+00:00

    Since you know exactly what you are searching for it would be easier NOT to use regX

    This is derived from Shyam Pillai's page

    http://skp.mvps.org/vba.htm#.XbmVI-j7SUk

    Sub embolden()

    Dim oPres As Presentation

    Dim oSld As Slide

    Dim oShp As Shape

    Dim otxtRng As TextRange

    Dim oTmpRng As TextRange

    Set oPres = ActivePresentation

         For Each oSld In oPres.Slides

            For Each oShp In oSld.Shapes

                If oShp.HasTextFrame Then

            If oShp.TextFrame.HasText Then

                Set otxtRng = oShp.TextFrame.TextRange

                Set oTmpRng = otxtRng.Replace(FindWhat:=" v ", _

                    Replacewhat:=" !! ", WholeWords:=False, MatchCase:=False)

                      If Not oTmpRng Is Nothing Then oTmpRng.Font.Bold = True

                Do While Not oTmpRng Is Nothing

                    Set oTmpRng = otxtRng.Replace(FindWhat:=" v ", _

                                Replacewhat:=" !! ", _

                                After:=oTmpRng.Start + oTmpRng.Length, _

                                WholeWords:=False, MatchCase:=False)

                                If Not oTmpRng Is Nothing Then oTmpRng.Font.Bold = True

                Loop

           End If

        End If

            Next oShp

        Next oSld

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2019-10-30T12:32:51+00:00

    So just to clarify when I add the line below (immediately after oshp.TextFrame.TextRange = strInput), it bolds all the text and not what I want which is only to bold the text I've replaced.  And also I want to do more than just bold, I want to change the font size, make it highlighted in green, etc.

    oshp.TextFrame.TextRange.Font.Bold = msoTrue '  Problem is this bolds all text in the TextFrame on the slide, not just the modified text of "!!"

    Was this answer helpful?

    0 comments No comments