Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling - Batch open and saving of sequential files

Status
Not open for further replies.

tanuki3

Technical User
Dec 5, 2001
13
0
0
US
I'm at a loss on what is probably a simple error handling routine problem.

I am opening several text based files in one folder, running some text manipulation commands on them, and then saving them as Word 97 docs in another folder.

The file names are numeric and sequential, but not all of them exist (e.g. 5.txt, 7.txt, 8.txt, 12.txt, etc.). I am trying to get the macro I have written to go to the next file number if it cannot find a given file number. This is what I have so far:

------------------------------
Sub

On Error GoTo File_Error

For intFileName = 1 to 999

'Word then opens the old file
Documents.Open FileName:= stFileName & ".txt" - etc.

'Word then performs several clean-up procedures in the file
'Word then saves the file as intFileName & ".doc"

File_Error:

Next intFileName

Exit Sub
----------------------------------

My difficulty is with the way my error handling routine handles the Documents.Open command. The sub will try 1, fail - but not generate an error, then 2 , then give me a run-time error on 2.

I believe the error handling routine is working the first time, but after that it stops.

What I need is for the Documents.Open command to keep trying numbers until it reaches one that exists. I think it may have something to do with the fact that the error handling routine is still active after it processes the first error, but I have experimented with the Resume GoTo command and various other things with no luck.

Any help would be greatly appreciated.



 
You may be better off with an on error resume next statement, and then checking the &quot;err&quot; object (default property is its number) right after the open method. If err<>0, then you can just loop right back, something like:

i=0
do
on error resume next
do
i=i+1
Documents.Open FileName:= &quot;no&quot; & i & &quot;.txt&quot;
loop until err=0 or i>999
if i<=999 then
...do your other stuff here
end if
loop until i>999


Make sense? (Your problem with the original code is most likely that the error object doesn't get cleared)
Rob
 
Note: my code above is a little sloppy - I meant to add an &quot;on error goto 0&quot; statement below the first &quot;loop&quot; to properly handle any other errors in the remainder of the code.
Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top