Consulting

Results 1 to 6 of 6

Thread: On Error GoTo line... doesn't work. Why?

  1. #1
    VBAX Regular
    Joined
    Jun 2016
    Posts
    11
    Location

    On Error GoTo line... doesn't work. Why?

    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
    Last edited by Dzony; 07-05-2016 at 08:01 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    On Error GoTo line1        
    UnpackFold = UnzipFold(StrFilename)
    Put the unpackfold = line before the On error line
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Jun 2016
    Posts
    11
    Location
    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"

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    Jun 2016
    Posts
    11
    Location
    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

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by Dzony View Post
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •