###Resource: Name1". I want to delete "###" from the above text using ppt vba. The text i..." /> ###Resource: Name1". I want to delete "###" from the above text using ppt vba. The text i..." /> ###Resource: Name1". I want to delete "###" from the above text using ppt vba. The text i..."/>

TextRange.Find().Delete method is deleting leading/trailing spaces in a textbox

325 views Asked by At

The text in the shape "TextBox 1" is "Work stack.<8Spaces>###Resource: Name1". I want to delete "###" from the above text using ppt vba. The text in the shape after the below statement gets executed is "Work stack. Resource: Name1".

MyPPT.Slides(1).Shapes("TextBox 1").TextFrame.TextRange.Find("###").Delete

It's deleting the spaces after the period but I want to keep those spaces i.e. the desired output is "Work stack.<8Spaces>Resource: Name1". Replace(FindWhat:="###", ReplaceWhat:=vbNullString) works really well but I wanted to check whether this is the default behavior of delete method or is it misbehaving or is there a way to change this behavior through ppt textbox properties or some other settings. Please share your thoughts.

1

There are 1 answers

2
BigBen On

This seems to be the intended behavior of Delete. When done manually the behavior is similar, i.e. if ### is selected in the textbox, ā†Backspace clears the leading white space. The behavior of Cut is similar.

Maybe use Replace with a blank string instead of Delete.

Sub Test()
    Dim MyPPT As Presentation
    Set MyPPT = ActivePresentation

    MyPPT.Slides(1).Shapes("TextBox 1").TextFrame.TextRange.Replace FindWhat:="###", ReplaceWhat:=""
End Sub