Log in

View Full Version : Wildcard searches (& Regular Expressions) in PowerPoint



fuzzynavel
08-11-2010, 04:27 AM
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

John Wilson
08-13-2010, 07:26 AM
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

fuzzynavel
09-08-2010, 11:38 AM
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

John Wilson
09-10-2010, 11:10 PM
You're welcome

vccbs
08-03-2013, 08:32 PM
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

John Wilson
08-03-2013, 11:30 PM
Glad it was useful.

Here's a link to our original page

Regular Expressions in PowerPoint (http://www.pptalchemy.co.uk/PowerPoint_RegEx.html)