[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Regular Expressions in PowerPoint

Published on
5,126 Points
2 Endorsements
Last Modified:

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.
1 Comment
LVL 48

Expert Comment

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.

Featured Post

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

This video teaches viewers how to add transitions to their Slideshows and how to set up timing for the transitions.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month