0

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 Answers1

1

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
BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Thanks a lot for you time but my sincere apologies for not rephrasing my question. I wanted to check whether "delete" method is misbehaving and if yes is there a workaround using "delete" method itself. I've used replace using vbNullString already which works really well. I'm really sorry for not mentioning that earlier. – Guru Sai Krishna Gangisetty Nov 16 '18 at 06:04
  • Doesn't seem like it's unintended. `Delete` behaves similarly to Backspace, if the `###` is selected first manually. `Cut` does the same thing (as seen also with Ctrl + X). – BigBen Nov 16 '18 at 06:43