How do I stop a Find Loop at the end of a file?

M

MikeZz

I want to run a loop to keep finding text from the beginning to end of a
document.
What's happening is when I use " .Wrap = wdFindContinue", it keeps going
through the document over and over and never stops until my "for" iterations
stop.

I also tried the following but get a compile error.
Do While Not EOF
....Search Code....
Loop

The best thing would be to either move cursur to the beginning and keep
iterating the find until it hits the end of file, or until it gets back to
where the cursor originally was.

THANKS!

for i = 1 to 100
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product/DRD FAM"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
next i
 
D

Dave Lett

Hi Mike,

Have a look at the following:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product/DRD FAM"
.Replacement.Text = ""
.Forward = True
Do While .Execute
'''insert your instructions here
Loop
End With

HTH,
Dave
 
G

Greg Maxey

Mike,

See if this helps you understand:

Sub Sample()
Dim myRange As Word.Range
Dim Count As Long
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = "Your text to find"
.MatchWholeWord = True
While .Execute
Count = Count + 1
myRange.Collapse Direction:=wdCollapseEnd
Wend
End With
MsgBox Count
End Sub
 
H

Helmut Weber

Hi Greg,

is there a misunderstanding,
or is something disconnecting us?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey

Helmut,

I am receiving and sending. No misunderstanding. Don't know about
connectivity.
 
M

MikeZz

Dave, Thanks for the help so far.
Your code works on it's own... but not when I put it into my nested mess.
Am I coding it correctly with the nexted With and loop statements?

Here's your recommended code with my abreviated loops shown...
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product/DRD FAM"
.Replacement.Text = ""
.Forward = True
Do While .Execute
======'''insert your instructions here - abreviated logic====
V V V V V V V V V V V V V
V V
With Selection.Find' # 1
End With
Selection.Find.Execute
'This With / End With repeated 3 more times
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^
======'''end of adding my abreviated logic====
Loop
End With

Here's my original code before adding your loops....

Sub FindPNsBackup()
Dim EWOPNs(500, 20) As String
eAction = 1 - 1
eYears = 2 - 1
eCurrentPart = 3 - 1
eNewPart = 4 - 1
eLF = 5 - 1
eFNADesc = 6 - 1
eVPPSDes = 7 - 1
eSide = 8 - 1
eMktDiv = 9 - 1
eColorCode = 10 - 1
eColorDesc = 11 - 1
eStk = 12 - 1
eServD = 13 - 1
eServI = 14 - 1
eNextUp = 15 - 1
eMakeFrom = 16 - 1
PNs = -1
'=============================================
'Finds info in first table of the part record.
'111111111111111111111111111111111111111111111
'=============================================
For x = 1 To 499
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product/DRD FAM"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Call MoveRightX(1)
If Trim(Selection.Text) = "Current Part" Then
PNs = PNs + 1
'True = Found Body of EWO.
Call MoveRightX(7)
EWOPNs(PNs, eAction) = Trim(Selection.Text)
Call MoveRightX(1)
EWOPNs(PNs, eYears) = Trim(Selection.Text)
Call MoveRightX(2)
EWOPNs(PNs, eCurrentPart) = Trim(Selection.Text)
Call MoveRightX(3)
EWOPNs(PNs, eNewPart) = Trim(Selection.Text)
End If
'=============================================
'Finds info in second table of the part record.
'222222222222222222222222222222222222222222222
'=============================================

Selection.Find.ClearFormatting
With Selection.Find
.Text = "UPC Prefix"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute
Call MoveRightX(3)
If Trim(Selection.Text) = "FNA Desc." Then
Call MoveRightX(2)
If Trim(Selection.Text) = "VPPS Description" Then
'Verified next section of EWO.
Call MoveRightX(4)
EWOPNs(PNs, eNextUp) = Trim(Selection.Text)
Call MoveRightX(1)
EWOPNs(PNs, eLF) = Trim(Selection.Text)
Call MoveRightX(4)
EWOPNs(PNs, eFNADesc) = Trim(Selection.Text)
Call MoveRightX(2)
EWOPNs(PNs, eVPPSDes) = Trim(Selection.Text)
End If
End If

'=============================================
'Finds info in third table of the part record.
'333333333333333333333333333333333333333333333
'=============================================

Selection.Find.ClearFormatting
With Selection.Find
.Text = "L/R"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute
Call MoveRightX(2)
If Trim(Selection.Text) = "Series" Then
Call MoveRightX(1)
If Trim(Selection.Text) = "Body (Styles)" Then
'Verified next section of EWO.
Call MoveRightX(6)
EWOPNs(PNs, eSide) = Trim(Selection.Text)
Call MoveRightX(1)
EWOPNs(PNs, eMktDiv) = Trim(Selection.Text)
End If
End If

'=============================================
'Finds info in forth table of the part record.
'444444444444444444444444444444444444444444444
'=============================================

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Engineer Code"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute
Call MoveRightX(8)
If Trim(Selection.Text) = "SERV Interchange" Then
'Verified next section of EWO.
Call MoveRightX(3)
EWOPNs(PNs, eColorCode) = Trim(Selection.Text)
Call MoveRightX(1)
EWOPNs(PNs, eColorDesc) = Trim(Selection.Text)
Call MoveRightX(1)
EWOPNs(PNs, eMakeFrom) = Trim(Selection.Text)
Call MoveRightX(2)
EWOPNs(PNs, eStk) = Trim(Selection.Text)
Call MoveRightX(1)
EWOPNs(PNs, eServD) = Trim(Selection.Text)
Call MoveRightX(1)
EWOPNs(PNs, eServI) = Trim(Selection.Text)
End If
Next x
End Sub
 

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