Problem with my find text routine.

S

sean

Hi All.

Thanks for all the previous help. Now for my next problem.

Synopsis: I have a find that searches a range. The range is the whole
document at the start. I have a find statement that searches for text at
the beginning of the line until the next occurrence of the simular text at
the next paragraph. There is always several paragraphs between the start
and end of the find range. The problem is that I don't want to include the
text at the end of the find. Lets use the following example text:

*** example starts ***

document.zip - a document
description of document.


document2.zip document two
description of text


document3.zip - document 3.
etc.

*** example ends ***

I only want to have the document 1 paragraph and the description of the
document in the range, nothing else. Then when the find starts again, it
has to start before the document2 paragraph. I am skipping the second
document and starting the third document. I know that I am screwing up the
ranges again. Code below:

Sub ConvertFile()
Dim oDocument As Document
Dim oRange As Range
Dim sDocumentName As String

sDocumentName = ActiveDocument.Name
Set oDocument = Documents(sDocumentName)
Set oRange = oDocument.Range
With oRange.Find
.Forward = True
.ClearFormatting
.Format = False
.MatchAllWordForms = False
.MatchCase = False
.MatchSoundsLike = False
.MatchWholeWord = False
.MatchWildcards = True
.Text = "^13[! ]@.ZIP*^13[! ]@.ZIP"
Do While .Execute
' moves the range to the beginning of the previous paragraph
oRange.MoveEnd Unit:=wdParagraph, Count:=-1
' removes any paragraph mark at the beginning of range.
oRange.MoveStart Unit:=wdParagraph
oRange.Move Unit:=wdParagraph, Count:=2
oRange.Collapse Direction:=wdCollapseEnd
MsgBox oRange.Text
Loop
End With
End Sub

Please help in straitening out the code above.

Regards
Sean
 
C

Cindy Meister -WordMVP-

Hi Sean,
Please help in straitening out the code
Well, I'd start by picking up your document object
directly:

Set oDocument = ActiveDocument

Then, if you're collapsing the range it's "of course" going
to be difficult to get back to the end of the "found"
range. (I think I'm interpreting what you want, and what
your code is doing, correctly. But I could be wrong!)

Try creating a second range to "mark your place". After the
merge .Execute is completed, the range object variable now
contains what was found. so...
Dim rngFound as Range
Set rngFound = range.Duplicate
rngFound.Collapse wdCollapseEnd
rngFound.End = ActiveDocument.Range.End

At this point, rngFound should extend from the END of the
last find to the end of the document. After you're done
with your manipulates of the range object variable, doing
whatever it is you need to with the found text:
Set range = rngFound.Duplicate

before you loop back for the next "Find"

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 

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