Consulting

Results 1 to 6 of 6

Thread: Wildcard searches (& Regular Expressions) in PowerPoint

  1. #1

    Wildcard searches (& Regular Expressions) in PowerPoint

    All,

    I am looking to perform a wildcard search and replace in a PowerPoint 2007 macro. I wish to find text between 2 strings on every slide and remove the bounding strings and then do some formatting of the text. Here is an example of text bounded by special strings on both sides as found on every slide:

    +++ Subtitle 1 ###

    So I want to search for the text (in this case, Subtitle 1) in between the +++ and ### strings. In a Word macro, I might do something like this with Selection.Find:

    .Text = "(+++)(?@)(###)"
    .Replacement.Text = "\2"

    Can something like this be done in a PowerPoint macro?


    Thanks,

    ~ Fuzzy

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    You can use RegEx in PowerPoint either by setting a reference to Microsoft vbscript Regular Expressions 55 or as here by late binding.
    Sub use_regex()
    Dim regX As Object
    Dim osld As Slide
    Dim oshp As Shape
    Dim strInput As String
    Dim b_found As Boolean
    
    Set regX = CreateObject("vbscript.regexp")
    With regX
    .Global = True
    'Note + is a special character so \+
    .Pattern = "\+\+\+(\w+)###"
    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, "$1")
    oshp.TextFrame.TextRange = strInput
    End If
    End If
    End If
    
    Next oshp
    Next osld
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    John,

    Sorry it has taken me this long to respond. Thank you for this solution... it is very clean and exactly what I was looking for!!! It's amazing how few references I could find about using regular expressions in PPT macros.

    Thanks again.

    Best,

    ~ Fuzzy

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    You're welcome
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Newbie
    Joined
    Aug 2013
    Posts
    1
    Location

    changing text in the Notes Pane of PowerPoint using regular expression notation

    A wonderfully useful thread! Thanks!

    Notes sometimes need wildcard search and replace. A wildcard search and replace (using regular expression notation) in the Notes Pane, instead of the slide, in PowerPoint needs a slightly modified approach compared to the above code from John Wilson.

    Below is Visual Basic code for modifying within the Notes Pane (or notes panel). The code changes a comma to a decimal point between number strings, as is needed when converting European notation 234,56 to UK/US notation 234.56. The code is modified from PPTAlchemy page PowerPoint_RegEx.html. You can find the page with a Google search of "Regular Expressions in PowerPoint" with PPTAlchemy.

    An important modification is to access the Notes Pane instead of the slide with ActivePresentation.Slides(intSlide).NotesPage, where intSlide is the integer number of the slide, instead of using ActivePresentation.Slides. A loop is added to sequentially process the slides from 1 to ActivePresentation.Slides.Count.

    The Visual Basic code can be modified for other strings as needed. For example, modifying
    .Pattern = "(\d)\,(\d)"
    to
    .Pattern = "(##)(.+)(##)"
    identifies arbitrary strings for changing in all Notes Panes in the PowerPoint file when the strings are placed between a beginning "##" and an ending "##" as in
    ##This string is identified for changing.##



    Sub use_regex_decimal_point()
    Dim regX As Object
    Dim osld As Slide
    Dim oshp As Shape
    Dim intSlide As Integer
    Dim strInput As String
    Dim b_found As Boolean
    Dim iRow As Integer
    Dim iCol As Integer
    Set regX = CreateObject("vbscript.regexp")
    With regX
    .Global = True
    .Pattern = "(\d)\,(\d)"
    End With

    For intSlide = 1 To ActivePresentation.Slides.Count
    For Each osld In ActivePresentation.Slides(intSlide).NotesPage
    For Each oshp In osld.Shapes
    If oshp.HasTable Then
    For iRow = 1 To oshp.Table.Rows.Count
    For iCol = 1 To oshp.Table.Columns.Count
    strInput = oshp.Table.Cell(iRow, iCol).Shape.TextFrame.TextRange.Text
    b_found = regX.Test(strInput)
    If b_found = True Then
    strInput = regX.Replace(strInput, "$1.$2")
    oshp.Table.Cell(iRow, iCol).Shape.TextFrame.TextRange = strInput
    End If
    Next iCol
    Next iRow
    Else
    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, "$1.$2")
    oshp.TextFrame.TextRange = strInput
    End If
    End If
    End If
    End If
    Next oshp
    Next osld
    Next intSlide
    Set regX = Nothing
    End Sub
    Last edited by vccbs; 08-03-2013 at 08:49 PM.

  6. #6
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Glad it was useful.

    Here's a link to our original page

    Regular Expressions in PowerPoint
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •