PDA

View Full Version : On Error GoTo line... doesn't work. Why?



Dzony
07-05-2016, 06:02 AM
Hi, I have problem with On Error statement

When I use my code sometimest there is no file that I need (StrFilename2, and StrFilename3) and I need an error statement. First statement works ok (On Error GoTo line1), but second not (On Error GoTo Line2), could you tell me where is problem from this part of my code?
I will be grateful for your response
Jan



If ....
code....

Else

On Error GoTo line1
UnpackFold = UnzipFold(StrFilename)
StrFilename2 = UnpackFold & "file.docx"

Open StrFilename2 For Input As #iFile
Do Until EOF(1)
Input #1, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile
line1:

On Error GoTo line2
StrFilename3 = UnpackFold & "file2.docm"
Open StrFilename3 For Input As #iFile
Do Until EOF(1)
Input #1, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile
line2:
MsgBox "Do not recognize extension"
End If

SamT
07-05-2016, 07:32 AM
On Error GoTo line1
UnpackFold = UnzipFold(StrFilename)
Put the unpackfold = line before the On error line

Dzony
07-05-2016, 08:13 AM
Thank you for reply SamT. I did as you said but it is the same as before. When try to open StrFilename3 there is error. "Run-time error '53'. File not found".
If error statement would work, there would not be problem.




Else

UnpackFold = UnzipFold(StrFilename)
On Error GoTo line1
StrFilename2 = UnpackFold & "file.docx"

Open StrFilename2 For Input As #iFile
Do Until EOF(1)
Input #1, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile
line1:

On Error GoTo line2
StrFilename3 = UnpackFold & "file2.docm"
​Open StrFilename3 For Input As #iFile '<--------<<<
Do Until EOF(1)
Input #1, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile
line2:
MsgBox "Do not recognize extension"

Paul_Hossler
07-05-2016, 06:34 PM
I think you're inconsistent with your file handles: sometimes you use a variable iFile and sometimes you use the constant 1

1. You need/should use FreeFile() to make sure that the file handle (e.g. 1) is not in use

2. I've always preferred to put the On Error as close to the statement(s) that I think are likely to cause an expected possible error

Without having the preceding code, including UnzipFold (which I'm guessing is a function) these are only somethings to explore



Option Explicit
Sub test()
Dim StrFilename2 As String, StrFilename3 As String, UnpackFold As String
Dim iFile As Long
Dim strTextLine As String
If 1 = 2 Then
MsgBox "Nope"

Else
' UnpackFold = UnzipFold(StrFilename)
UnpackFold = "Something"
StrFilename2 = UnpackFold & "file.docx"

On Error GoTo line1
iFile = FreeFile
Open StrFilename2 For Input As #iFile
Do Until EOF(iFile)
Input #iFile, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile

line1:
StrFilename3 = UnpackFold & "file2.docm"
iFile = FreeFile

On Error GoTo line2
Open StrFilename3 For Input As #iFile
Do Until EOF(iFile)
Input #iFile, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile
line2:
MsgBox "Do not recognize extension"
End If
End Sub

Dzony
07-06-2016, 04:28 AM
Hi Paul
I have read that I can't use error handling blocks like I did ( http://www.cpearson.com/excel/errorhandling.htm ) so I have to figure out how to do it in other way.
Thank you for help.
Jan

Paul_Hossler
07-06-2016, 06:36 AM
Hi Paul
I have read that I can't use error handling blocks like I did ( http://www.cpearson.com/excel/errorhandling.htm ) so I have to figure out how to do it in other way.
Thank you for help.
Jan

I'd be interested in why you believe that you can't use error handling like that

Was there a specific reference in Chip's link?


Not knowing what your overall logic flow is, but maybe something like this



Option Explicit
Sub test()
Dim StrFilename2 As String, StrFilename3 As String, UnpackFold As String
Dim iFile As Long
Dim strTextLine As String

If 1 = 2 Then
MsgBox "Nope"

Else
' UnpackFold = UnzipFold(StrFilename)
UnpackFold = "Something"
StrFilename2 = UnpackFold & "file.docx"

On Error GoTo line1
iFile = FreeFile
Open StrFilename2 For Input As #iFile
Do Until EOF(iFile)
Input #iFile, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile
GoTo lbl_Exit

line1:
StrFilename3 = UnpackFold & "file2.docm"
iFile = FreeFile

On Error GoTo line2
Open StrFilename3 For Input As #iFile
Do Until EOF(iFile)
Input #iFile, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile
GoTo lbl_Exit
line2:
MsgBox "Do not recognize extension"
End If

lbl_Exit:
On Error GoTo 0
End Sub



It's a bit (a lot) spaghetti-ish with the GoTo's so there's better ways to structure it if we understood the logic and the goal