replacing a smaller string within a larger string

L

Larry

If there is a smaller string in a larger string, I want to delete the
smaller string. Is it possible to do this? I've tried simply making
smallString = "", but that hasn't worked yet.

' Test code

mainString = Selection.Text
smallString = "HELLO"

If InStr(mainString, smallString) > 0 Then
' What?

Thanks.
 
J

Jay Freedman

Use the Replace function, replacing the small string with nothing:

Selection.Text = Replace(Selection.Text, smallString, "")

If the small string doesn't occur in the selection, nothing will
happen (strictly speaking, the selection is replaced with itself). If
the small string occurs one or more times, all occurrences will be
removed at once.

The Replace function isn't available in Word 97 VBA, so if you need to
support that version, look in Google Groups for a way to write a
function that does the same thing.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
L

Larry

Thanks Jay.

Larry



Jay Freedman said:
Use the Replace function, replacing the small string with nothing:

Selection.Text = Replace(Selection.Text, smallString, "")

If the small string doesn't occur in the selection, nothing will
happen (strictly speaking, the selection is replaced with itself). If
the small string occurs one or more times, all occurrences will be
removed at once.

The Replace function isn't available in Word 97 VBA, so if you need to
support that version, look in Google Groups for a way to write a
function that does the same thing.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top