Selection.find keeps finding the first occurence

M

Matt Shaw

I'm trying to search a document and each time I find a target word I
want to select the whole sentence and cut and paste it into another
document; then continue to the next sentence with the target word.

I can get this to work but each time it finds the same first sentence
and won't go the "find next" point.

It's obviously something dumb I'm doing so anyone who can tell me the
error of my ways - please feed free My code looks like this --

Documents.Open Filename:="dest.doc"
Set Destination = ActiveDocument

Documents.Open Filename:="source.doc"
Set Source = ActiveDocument

target = "targetword"

With Selection.Find
.Text = target
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindStop
End With
Selection.Find.Execute

Selection.StartOf Unit:=wdSentence
Selection.Expand Unit:=wdSentence
sent = Selection()
destination.Activate
selection.insertafter sent & vbCR
Source.Activate
 
M

martinique

When you're working from code it is always better to use Range objects
instead of the Selection object. In this case, it means you can resume your
search from the same point. (As you currently have it, your selection
changes to the paste location each time, so your search re-starts.)
 
M

Matt Shaw

Martinique,

Thanks a lot for your help -- that's working great now. I still have a
few problems with sentences containing (or not containing) carriage
returns or page breaks etc but I can clean that up now.

Thanks for your patience with this dumb user.

Cheers

Matt
 

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