Link to home
Start Free TrialLog in
Avatar of Laszlo Benedek
Laszlo BenedekFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to find and replace with macro in powerpoint without affecting the formatting?

The code below can find and replace text within a powerpoint file, however it clears individual formatting of words within the text boxes. That is after running this code all part inside the text box will be formatted like the first character. I would like to find and replace without affecting the formating.

As an example the power point would have a textbox with the following text:

Introduction
text used for testing the macro[1] and has different formatting

This is the result when I run the code

Introduction
text used for testing the macro[2] and has different formatting


This is the expected, ideal result

Introduction
text used for testing the macro[2] and has different formatting

 Sub DataScrubAllSlidesAndTables()
    Dim OldTxt As String
    Dim NewTxt As String
    OldTxt = "[1]"
    NewTxt = "[2]"
    
 
 
    Dim sld As Slide
    Dim grpItem As Shape
    Dim shp As Shape
    Dim i As Integer
    Dim j As Integer
    Dim varTemp As Variant
    For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes

        If shp.HasTextFrame Then
            If shp.TextFrame.HasText Then
                shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, OldTxt, NewTxt)
            End If
        End If

    

    Next shp
Next
End Sub

Open in new window

Avatar of Laszlo Benedek
Laszlo Benedek
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Does exactly what's needed and it was quite quick, thanks.
Hello, Rgonzo1971

the .Delete part inserts an unnecessary space when I only replace a part of a word instead of the whole word.

Please see the related question.

https://www.experts-exchange.com/questions/29020774/Powerpoint-macro-TextFrame-TextRange-character-place-length-delete-creates-a-space-in-the-place-of-the-deleted-part.html