4

I would like to replace the word "hello" with "world" on slide 1 of the ppt. How can I do that using VBA script.

3
  • 1
    I suggest you read something like Getting Started with VBA in PowerPoint 2010, then try writing a bit of code yourself. If you get stuck, show us what you've done so we can help. Commented Mar 2, 2017 at 12:51
  • Actually, I'm new to VBA and I already did some research before, however I'm not find the proper information that I need. Could you please share example? Commented Mar 2, 2017 at 13:32
  • Check out this sample: skphub.com/ppt00025.htm#2 Commented Aug 10, 2017 at 2:51

2 Answers 2

11
Sub findAndReplaceText()
Dim sld As Slide
Set sld = ActivePresentation.Slides(1)
Dim shp As Shape
For Each shp In sld.Shapes
If shp.HasTextFrame Then
    If shp.TextFrame.HasText Then
        shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "hello", "world")
    End If
End If
Next shp
End Sub

Reference: https://www.youtube.com/watch?v=BYfKvVmtAGE

Sign up to request clarification or add additional context in comments.

3 Comments

Note that this will not find/replace text in tables, smartart, grouped shapes, charts etc.
Note that this will not maintain any line or section specific formatting, as it replaces all of the text in the shape
additionally to checking the HasText, you will also want to avoid grouped items as they will fail on TextRange. -> If shp.TextFrame.HasText And shp.Type <> msoGroup Then
2

In a slight addendum to the previous answer, in office 2016, I used the following method to achieve the same thing, however this method lets me keep formatting within the text range, as it only replaces the specific text searched for:

Sub findAndReplaceText(sld As PowerPoint.Slide, findText As String, replaceText As String)
Dim shp As PowerPoint.Shape
Dim textLoc As PowerPoint.TextRange
For Each shp In sld.Shapes
    If shp.HasTextFrame Then
        If shp.TextFrame.HasText Then
           Set textLoc = shp.TextFrame.TextRange.Find(findText)  'use Find function to get the textrange for the string being searched for
           If Not (textLoc Is Nothing) Then 'if something is found
            textLoc.Text = replaceText      'then replace it
           End If
        End If
    End If
Next shp
End Sub

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.