2

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?

wh1t3cat1k
  • 3,146
  • 6
  • 32
  • 38

1 Answers1

3

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
Steve Rindsberg
  • 3,470
  • 1
  • 16
  • 10
  • VBA. Sorry, forgot to mention that you'd have to translate to .NETness, along the lines of ... = shape.TextFrame.TextRange.Replace(oldString, replacementString); In this case, TextRange.Replace is a method; it does the replacement directly on the object, unlike .Text.Replace, which returns a string but doesn't affect the .TextRange.Text – Steve Rindsberg Jul 25 '11 at 13:38