stilts77
stilts77

Reputation: 191

How To Search and Replace Text in ActiveX TextBox

I am trying to search an ActiveX TextBox (TextBox1) to replace a phrase with nothing...

I have this code that seems to just wipe the entire box rather than the phrase in isolation.

Private Sub CommandButton3_Click()


TextBox1 = Selection


Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "This is the text to remove!"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll


End Sub

With some additions (like selecting all shapes in the active document)- the code works with an ordinary TextBox - and with the rest of the document too... just not the ActiveX box (which is what I want!!)

Please help!

Upvotes: 0

Views: 2365

Answers (1)

Joshua Honig
Joshua Honig

Reputation: 13235

You can simply use the built-in VBA Replace function:

Private Sub CommandButton3_Click()
    TextBox1.Value = Replace(TextBox1.Value, "This is the text to remove!", "")
End Sub

Upvotes: 2

Related Questions