Trying to jump to Beginning of file--Iterating multiple times

S

StarfishJoe

I'm creating a macro in WORD using VBA that opens two text files (.txt).
Text File #1 contains a list of data that I will be searching, and Text
File#2 will be written to or appended to based on what the procedure finds in
Text File #1 .

The parent Word Document will contain a form and will hold the summary
results from iterating through the text files.(Count1, count2, count3 etc.)

My problem is this: I can successfully iterate through one time using the
following loop, however, I have more than one string to search on, and need
to go back to the beginning of the file to start the second loop. (Iknow that
I can use a nested loop and an array, instead of repeating the code here, but
I need to make this work one time this way, before I nest the loop, I think
i'll still have the same problem of not moving from EOF back to "Beginning of
file"

Do Until EOF(1)
Line Input #1, Data
If InStr(1, Data, TextToFind1) Then
Print #2, Data
count1 = count1 + 1
End If
Loop
' The first loop works fine, It finds all instances of the string
TextToFind1,
'and records them on Text File#2. Count1 holds the number of times
TextToFind1 was found,
' but cursor needs to go to Beginning of file here
Do Until EOF(1)
Line Input #1, Data
If InStr(1, Data, TextToFind2) Then
Print #2, Data
count2 = count2 + 1
End If
Loop
'and again, back to beginning of file
Do Until EOF(1)
Line Input #1, Data
If InStr(1, Data, TextToFind3) Then
Print #2, Data
count3 = count3 + 1
End If
Loop
'And again back to beginning of file here.here.
Do Until EOF(1)
Line Input #1, Data
If InStr(1, Data, TextToFind4) Then
Print #2, Data
count4 = count4 + 1
End If
Loop

'When all search strings are found, then I will receive a pop up telling me
the search is complete. This works fine too, but right now only the first
loop is working correctly.

EOF is a pre-defined key word in VBA, but BOF for beginning of file is not.
I tried "Goto BOF", and "GoTo BOF(1)" and this didn't work.

How do I tell it to Go back to the beginning of the file?
 
D

Doug Robbins - Word MVP

Selection.HomeKey wdStory

or

Dim myrange As Range
Set myrange = ActiveDocument.Range
myrange.Collapse wdCollapseStart
myrange.Select


--
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
 

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