Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
StarfishJoe - 16 Oct 2006 03:03 GMT
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?
Doug Robbins - Word MVP - 16 Oct 2006 04:41 GMT
Selection.HomeKey wdStory

or

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

Signature

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

> 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
[quoted text clipped - 65 lines]
>
> How do I tell it to Go back to the beginning of the file?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.