Using "If Not" with Find.Execute?

E

Ed

Running a macro that has run fine on a new document, it now throws an error.
The code is supposed to find some text, then capture the text immediately
following.

rngDoc2.Find.Execute FindText:="| 4. L5-"
rngDoc2.MoveEnd Unit:=wdWord, Count:=1
strDoc = rngDoc2.Text

For some reason, when the FindText was not there before, the macro would
end. Now, though, it gives me a paragraph mark as the string contents,
which screws up the rest of the macro (it wants to ceate a file name using
this string).

As this is in a Do loop, is it best to

If Not rngDoc2.Find.Execute FindText:="| 4. L5-" Then Exit Do

as a way to break out of the loop if the text isn't found, but continue if
it is? Is there a better way?

Ed
 
D

Doug Robbins - Word MVP

Better if you show us more of the code. The normal construction however
would be as follows:

Dim srange As Range
Dim rtext As String
rtext = InputBox("Enter the text to be inserted")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^t", MatchWildcards:=False, _
MatchCase:=False, Wrap:=wdFindStop, Forward:=True) = True
Set srange = Selection.Range.Duplicate
Selection.Collapse wdCollapseEnd
Selection.MoveRight wdCharacter, 1
If srange.Style = "TOC 1" Then
srange.Text = vbTab & rtext
End If
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Ed

Thanks for the reply, Doug. What I've got is a collection of separate
documents (database reports) all strung together in one large file. The
complete code loop searches for a text string which is at the end of each
report, sets a range form there back to the beginning of the file, and
splits that out as a separate document. (The text is actually deleted from
the conglomerate file, making the start of the next report the beginning of
the file.) The Find.Execute I asked about is searching for a specific text
string which will be used as the name of this new document. If this string
is not found, then I have garbage at the end of the file that can be
ignored.

Talking through this, though, I think my real problem is why isn't my code
recognizing that it doesn't have a valid file? This code searches for the
end marker, then sets the range to the beginning:

Do

' Find end of doc
Set rngDoc = rngFile ' Sets range to entire file contents
rngDoc.Find.Execute _
FindText:="Form XXXX-E: "
' Move range to cover all of doc
rngDoc.MoveEnd Unit:=wdParagraph
rngDoc.MoveStart wdStory, -1

If there's garbage, it shouldn't find "Form XXXX-E: ". Right there is
where I need to put my "If not found, exit Do". By your example, would this
be better as:

Set rngDoc = rngFile ' Sets range to entire file contents
Do While rngDoc.Find.Execute _
FindText:="Form XXXX-E: "
' Move range to cover all of doc
rngDoc.MoveEnd Unit:=wdParagraph
rngDoc.MoveStart wdStory, -1

Ed
 
H

Helmut Weber

Hi Ed,

everybody has it's own style.

This would be my approach:

Sub Test0002()
Dim rngDcm As Range
Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Text = "Form XXXX-E: *^13"
.MatchWildcards = True
While .Execute
rngDcm.start = 0
rngDcm.Select ' for testing remove afterwards
' your code
rngDcm.Delete
Wend
End With
End Sub
' -------------------------

Here and now

rngDoc.MoveEnd Unit:=wdParagraph

failed, if there was a section break
between the found spot and the next paragraph mark.
The section break was regarded as a paragraph. :-(


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Ed

Hi, Helmut. Good to hear from you again! The section break won't be an
issue for this doc.

Two questions:
.Text = "Form XXXX-E: *^13" - Doesn't the * allow for any character
and any number of characters? Will this definitely stop at the first
paragraph after the Found text?
rngDcm.start = 0 - This is equivalent to .MoveStart?

Ed
 
E

Ed

Thanks so much, Helmut.

Cheers!
Ed

Helmut Weber said:
Hi Ed,


Haven't come across a case when it didn't in ordinary text.
Doesn't work, if "xxxx..." is in a table.
At least not across cell borders.


At least it seems so.

It very hard to say, if two or more ways,
which show the same result,
will ever and always do so.

--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000 (german versions)
 

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