Table of contents
TOC
Collapse the table of content
Expand the table of content

TextRange.Replace Method (PowerPoint)

office 365 dev account|Last Updated: 6/12/2017
|
1 Contributor

Finds specific text in a text range, replaces the found text with a specified string, and returns a TextRange object that represents the first occurrence of the found text. Returns Nothing if no match is found.

Syntax

expression. Replace( FindWhat, ReplaceWhat, After, MatchCase, WholeWords )

expression A variable that represents a TextRange object.

Parameters

NameRequired/OptionalData TypeDescription
FindWhatRequiredStringThe text to search for.
ReplaceWhatRequiredStringThe text you want to replace the found text with.
AfterOptionalIntegerThe position of the character (in the specified text range) after which you want to search for the next occurrence of FindWhat. For example, if you want to search from the fifth character of the text range, specify 4 for After. If this argument is omitted, the first character of the text range is used as the starting point for the search.
MatchCaseOptionalMsoTriStateDetermines whether a distinction is made on the basis of case.
WholeWordsOptionalMsoTriStateDetermines whether only whole words are found.

Return Value

TextRange

Remarks

The MatchCase parameter value can be one of these MsoTriState constants.

ConstantDescription
msoFalseThe default. Does not distinguish between uppercase and lowercase characters.
msoTrueDistinguish between uppercase and lowercase characters.

The WholeWords parameter value can be one of these MsoTriState constants.

ConstantDescription
msoFalseThe default. Does not find only entire words.
msoTrueFinds only entire words.

Example

This example replaces every whole-word occurrence of "like" in all of the shapes in the active presentation with "NOT LIKE".

Sub ReplaceText()



    Dim oSld As Slide

    Dim oShp As Shape

    Dim oTxtRng As TextRange

    Dim oTmpRng As TextRange



    Set oSld = Application.ActivePresentation.Slides(1)



    For Each oShp In oSld.Shapes

        Set oTxtRng = oShp.TextFrame.TextRange

        Set oTmpRng = oTxtRng.Replace(FindWhat:="like", _
            Replacewhat:="NOT LIKE", WholeWords:=True)

        Do While Not oTmpRng Is Nothing
            Set oTxtRng = oTxtRng.Characters(oTmpRng.Start + oTmpRng.Length, _
                oTxtRng.Length)

            Set oTmpRng = oTxtRng.Replace(FindWhat:="like", _
                Replacewhat:="NOT LIKE", WholeWords:=True)
        Loop

    Next oShp



End Sub

See also

Concepts

TextRange Object

© 2018 Microsoft