Search code examples
office-interopstr-replacepowerpoint-2007

Replacing some text in PowerPoint


I use Microsoft.Office.Interop.PowerPoint to replace some specific token at each slide from a *.pptx presentation.

The problem is that the text box in which the token resides has lines which are formatted in different ways (e.g. lines with different font size).

I actually tried doing the replacement by both

shape.TextFrame.TextRange.Text = strStartText + replacementString + strEndText;

and

shape.TextFrame.TextRange.Text = 
    shape.TextFrame.TextRange.Text.Replace(oldString, replacementString);

But it unifies and thus spoils all the formatting of my textbox. All the lines and words are now having the same size/colour etc.

Is there any solution to this?


Solution

  • PowerPoint's .TextRange objects have a .Replace method that works similarly to VB/VBA's Replace command, but it preserves formatting.

    Example, assuming you have a reference to the shape in the variable oSh:

    With oSh
        With .TextFrame.TextRange
            .Replace findwhat:=oldString, replacewhat:= replacementString
        End With
    End With