Haho
asked on
Error handling ... how to improve it?
Hi,
I am reading a list of files from a listbox and then printing each one. I added some error handling to handle missing files, which will continue with the next file to print. However, I am not very clear with the error handling of VB and it seems my code can be improved.
Soemtimes, when there is a error (listbox is empty), if I press Print again, there is a error "For loop not initialised". I know it has to do the 'nextF' statement but I am not sure how to improve/ correct it.
I provided main parts of my print function...
sorry, I don't have much points left... :(
Thanks a lot!
David
Private Sub Print_Click()
' declarations
On Error GoTo handleErr
For i = 0 To (List1.ListCount - 1) ' read from list1
On Error Resume Next
Open (list1.text) For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
' print line
Loop
nextF:
' close file
Next i
End If ' response = vbyes
Exit Sub 'avoid error handler
handleErr:
Select Case Err.Number ' Evaluate error number.
Case 32755 ' printer error
MsgBox ("Error printing. Printing aborted.")
Exit Sub
Case 53: ' file not found
MsgBox ("File " & ToPrint & " not found. Continuing with next file.")
GoTo nextF
End Select
Err.Clear
End Sub
I am reading a list of files from a listbox and then printing each one. I added some error handling to handle missing files, which will continue with the next file to print. However, I am not very clear with the error handling of VB and it seems my code can be improved.
Soemtimes, when there is a error (listbox is empty), if I press Print again, there is a error "For loop not initialised". I know it has to do the 'nextF' statement but I am not sure how to improve/ correct it.
I provided main parts of my print function...
sorry, I don't have much points left... :(
Thanks a lot!
David
Private Sub Print_Click()
' declarations
On Error GoTo handleErr
For i = 0 To (List1.ListCount - 1) ' read from list1
On Error Resume Next
Open (list1.text) For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
' print line
Loop
nextF:
' close file
Next i
End If ' response = vbyes
Exit Sub 'avoid error handler
handleErr:
Select Case Err.Number ' Evaluate error number.
Case 32755 ' printer error
MsgBox ("Error printing. Printing aborted.")
Exit Sub
Case 53: ' file not found
MsgBox ("File " & ToPrint & " not found. Continuing with next file.")
GoTo nextF
End Select
Err.Clear
End Sub
HI
Maybe create a sub funcion that calls the error handler every time there is an error
on error goto eh:
..
..
..
..
eh:
'call your function and in your function make sure you have the resume next statement
hope this helps
craig
Maybe create a sub funcion that calls the error handler every time there is an error
on error goto eh:
..
..
..
..
eh:
'call your function and in your function make sure you have the resume next statement
hope this helps
craig
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks !!! For the time being I am doing:
for i ...
open file
if err.number <> 0 then
err msgbox
else
printing code
end if
next i
for i ...
open file
if err.number <> 0 then
err msgbox
else
printing code
end if
next i
for the error handling you can use Err.description and err.number to know all the error posible without a huge Select case with all the error you can check if list1.listindex = -1 if there's no item selected