Find between bookmarks

R

RRodrigues

Hi,

I've a problem with a code, to find some words between bookmarks and copy
the whole line to another document.

Here’s my code:

Dim rngBkm As Range

Set rngBkm = ActiveDocument.Range( _
Start:=ActiveDocument.Bookmarks("PrejSt").Start, _
End:=ActiveDocument.Bookmarks("PrejEnd").Range.End)

rngBkm.Select

With rngBkm.Find
.ClearFormatting

Do While rngBkm.Find.Execute(FindText:="Word", Format:=True, Forward:=True)

If .Found = True Then

With .Parent
.Select
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Windows("Document3").Activate
Selection.PasteAndFormat (wdPasteDefault)
Windows("Relcomp.doc").Activate
Selection.MoveRight Unit:=wdCharacter, Count:=1

End With

Else
Exit Do
End If

Loop

End With



However, the code doesn’t stop to find at the end of the range, but only in
the end of the document.

Can you help me with this?
 
H

Helmut Weber

Hi Rodrigues,

controlling range.find is tricky.
The range collapses to the found spot and expands again,
sometimes this way sometimes that way.
Sometimes you don't have to do anything about it,
sometimes you have to control it yourself.


Sub Test567()
Dim rTmp As Range
Set rTmp = Selection.Range
rTmp.Start = ActiveDocument.Bookmarks("bookmark1").Start
rTmp.End = ActiveDocument.Bookmarks("bookmark2").End
With rTmp.Find
.Text = "quick"
.Wrap = wdFindStop
While .Execute
rTmp.Select
MsgBox rTmp.Bookmarks("\line").Range.Text
' here comes the copying and pasting to the other doc
' which is just one of some possible ways to do what you want
rTmp.Start = Selection.End '<
rTmp.End = ActiveDocument.Bookmarks("bookmark2").End '<
Wend
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

RRodrigues

Thank you Helmut!

It worked fine!

In fact, I've tried several other ways to control the range.find but, with
all, I’ve the same result… it searches until the end of the document.

Thank you again!

… and greetings from Lisbon, Portugal!

Ricardo Rodrigues
 

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