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?
Helmut Weber - 01 May 2006 09:54 GMT
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

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
RRodrigues - 01 May 2006 23:19 GMT
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
> Hi Rodrigues,
>
[quoted text clipped - 24 lines]
>
> HTH