Regular Expressions in PowerPoint

John WilsonCEO PowerPoint Alchemy

Regular Expressions

Microsoft Word has sophisticated search tools that can search for patterns. For example if you wanted to search for all UK phone numbers that followed a pattern of five digits, a space and then six digits you can easily do this in Word using Advanced Wild Card searches.

The search would look like this:

regX1.jpgThe find what box search pattern is any digit 0-9 five times, followed by a space and then any digit 0-9 six times. "Under the hood" Word is utilizing Regular Expressions to search for a pattern.

Unfortunately PowerPoint has very basic search tools and a search for a pattern is not available from the UI.

Search With Code

Look at this code:
Sub use_regex()
Dim regX As Object
Dim oMatch As Object
Dim osld As Slide
Dim oshp As Shape
Dim strInput As String
Dim b_found As Boolean
Dim strReport As String
Dim i As Integer
Dim iFileNum As Integer
Dim b_First As Boolean
Dim b_start As Boolean
Dim strpattern As String

On Error Resume Next
Set regX = CreateObject("vbscript.regexp")
strpattern = "[0-9]{5} [0-9]{6}"
With regX
    .Global = True
    .Pattern = strpattern
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
                    If Not b_start Then
                        b_start = True
                        strReport = strReport & "Results" & vbCrLf
                    End If
                    strReport = strReport & vbCrLf & "On Slide " & osld.SlideIndex & ": "
                    Set oMatch = regX.Execute(strInput)
                    For i = 0 To oMatch.Count - 1
                        strReport = strReport & oMatch(i) & " / "
                    Next i
                    b_First = False
                    If Right(strReport, 2) = "/ " Then strReport = Left(strReport, Len(strReport) - 2)
                End If
            End If
        End If
    Next oshp
Next osld
If strReport = "" Then strReport = "NOT FOUND!"
iFileNum = FreeFile
Open Environ("USERPROFILE") & "\Desktop\Abbr.txt" For Output As iFileNum
Print #iFileNum, strReport
Close iFileNum
Call Shell("NOTEPAD.EXE " & Environ("USERPROFILE") & "\Desktop\Abbr.txt", vbNormalFocus)
Set regX = Nothing
End Sub

Open in new window

The code declares the variables needed including regx as an Object.
It then creates a regex object using this line:

Set regX = CreateObject("vbscript.regexp")

The pattern to search for is set here:

strpattern = "[]0-9]{5} []0-9]{6}"

The code then loops through all of the slides and shapes with text looking for matches to the pattern.

Everytime a match is found the slide number and matching text is added to the variable strReport.

Finally a text file with the strReport is created. This file can be saved or printed for reference.

Adapt The Code

To perform different searches you just need to change the line:

strpattern = "[]0-9]{5} []0-9]{6}"

For example to search for a US number in this format 800-111-2222 you might use "[]0-9]{3}-[]0-9]{3}-[]0-9]{4}"

Searching For Non Numeric Items

Regex allows you to use very complex search patterns to find almost any pattern. Building these patterns is beyond the scope of this article but see "Find Out More".

Here are some simple examples:

strpattern = "[A-Z(.]{2,}"

This looks for CAPITAL letters [A-Z]
Followed by an OPTIONAL period (.)
With 2 or mor repeats {2,}

You could use this to find abbreviations e.g. BBC or UNESCO

strpattern = "[A-Z][a-z]{2,} [A-Z][a-z]{2,}"

Is searching for a capital followed by 2 or more lower case letters, a space and then another capital followed by 2 or more lower case letters.

You could use this to form a simplistic search for Names in the form Joe Bloggs. You would need to adapt it in more complex circumstances to e.g find Bill McGovern or J.Bloggs.


Find Out More

You could refer to the following for background information regarding regular expressions:

Searching Google for Regular Expressions will also help you create more complex search patterns.
John WilsonCEO PowerPoint Alchemy

Comments (1)

aikimarkGet vaccinated; Social distance; Wear a mask
Top Expert 2014

You need to format your code.  Without indentations, it is difficult to read.  I certainly wouldn't want to add it/use it in any VBA project in its current state.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community