select text between searches

M

MikeC

I want to search for WORD1, then search for WORD2, and
select all the text, including WORD1, up to (but not
including) WORD2, with a view to deleting it.

Any ideas welcome.

Tx, MikeC
 
D

Dave Lett

Hi MikeC,

You can use something like the following:

Dim oRng As Range
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "Word1"
If .Execute Then
Set oRng = Selection.Range
Selection.MoveRight
Else
MsgBox .Text & " was not found.", vbInformation
Exit Sub
End If
.Text = "Word2"
If .Execute Then
oRng.End = Selection.Start
oRng.Select
Else
MsgBox .Text & " was not found.", vbInformation
Exit Sub
End If
End With
End With

HTH,
Dave
 
M

MikeC

Many thanks Dave,

I haven't tried this out yet, but it certainly seems
complete. I have used Range in Excel programs, but I
hadn't realised it could be used in Word, so I've just
learned something new.

Best Regards,

MikeC.
 
K

Klaus Linke

MikeC said:
I want to search for WORD1, then search for WORD2, and
select all the text, including WORD1, up to (but not
including) WORD2, with a view to deleting it.

Any ideas welcome.

Tx, MikeC


Hi Mike,

You could also do a wildcard search for
Find what: WORD1*(WORD2)
Replace with: \1

That deletes WORD1 and the text up to WORD2.
It keeps WORD2, since you put it in "Replace with".
\1 is the placeholder for the 1st expression in brackets -- "WORD2" in this
case.

Regards,
Klaus
 

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