Results 1 to 6 of 6

Thread: Error not being caught (RTE 76)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    27

    Error not being caught (RTE 76)

    RTE 76 doesn't want to be caught for some reason. Some one mind helping with the problem?
    My Code
    Code:
    Public Sub DownloadFile(ByVal URL As String, ByVal Path As String)
        Dim fBytes() As Byte
        Dim fileNum As Long
        
        On Error GoTo Err
    
        fBytes() = Form1.inetDownload.OpenURL(URL, icByteArray)
    
        If FileExists(Path) Then Kill (Path)
        
    start:
        fileNum = FreeFile
        Open Path For Binary Access Write As #fileNum
        Put #fileNum, , fBytes()
        Close #fileNum
        Exit Sub
        
    Err:
        If Err.Number = 76 Then
            Call MkDir(Mid$(Path, 1, Len(Path) - InStrRev(Path, "/", , vbTextCompare)))
            Err.Clear
            start
        End If
    End Sub
    Please help
    P.S Typed an informative txt. Browser crashed. Don't feel like doing it again. But if you didn't understand I'll be glad to expand then.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Error not being caught (RTE 76)

    Are you sure you suppose to get err.number 76?
    Comment out "If Err.Number = 76 Then" and do debug.print err.number instead so you can get actual error number.
    Then make your decision how to handle it based on what you find.

    Also, few comments regarding your code:

    - try not to use labels (start, etc...)
    - try not to use reserved words (Err) for anything you do within your code...

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Error not being caught (RTE 76)

    Try the following modifications:

    Code:
    Public Sub DownloadFile(ByRef sURL As String, ByRef sFilePath As String)
        Dim bytData() As Byte
        Dim FN        As Integer
        Dim sDir      As String
    
        bytData() = Form1.inetDownload.OpenURL(sURL, icByteArray)
    
       'Extract the path component of the fully-qualified file path
        sDir = Left$(sFilePath, InStrRev(sFilePath, "\") - 1&)
    
        If Not PathExists(sDir) Then
            MkDirEx sDir '<-- Click this
        ElseIf FileExists(sFilePath) Then
            Kill sFilePath
        End If
    
        FN = FreeFile
        Open sFilePath For Binary Access Write As FN
            Put #FN, , bytData()
        Close FN
    End Sub
    
    Public Function FileExists(ByRef sFile As String) As Boolean
        On Error Resume Next
        FileExists = (GetAttr(sFile) And vbDirectory) <> vbDirectory
    End Function
    
    Public Function PathExists(ByRef sPath As String) As Boolean
        On Error Resume Next
        PathExists = (GetAttr(sPath) And vbDirectory) = vbDirectory
    End Function
    You may also want to check out the URLDownloadToFile API function. Search this forum for examples of using it.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    27

    Re: Error not being caught (RTE 76)

    Quote Originally Posted by RhinoBull View Post
    Are you sure you suppose to get err.number 76?
    Comment out "If Err.Number = 76 Then" and do debug.print err.number instead so you can get actual error number.
    Then make your decision how to handle it based on what you find.

    Also, few comments regarding your code:

    - try not to use labels (start, etc...)
    - try not to use reserved words (Err) for anything you do within your code...
    The error isn't being handled so I don't see how commenting out the if statement will have any effect. (I went line by line so I am pretty sure the label isn't being reached.)
    Try the following modifications:

    Code:
    Public Sub DownloadFile(ByRef sURL As String, ByRef sFilePath As String)
    Dim bytData() As Byte
    Dim FN As Integer
    Dim sDir As String

    bytData() = Form1.inetDownload.OpenURL(sURL, icByteArray)

    'Extract the path component of the fully-qualified file path
    sDir = Left$(sFilePath, InStrRev(sFilePath, "\") - 1&)

    If Not PathExists(sDir) Then
    MkDirEx sDir '<-- Click this
    ElseIf FileExists(sFilePath) Then
    Kill sFilePath
    End If

    FN = FreeFile
    Open sFilePath For Binary Access Write As FN
    Put #FN, , bytData()
    Close FN
    End Sub

    Public Function FileExists(ByRef sFile As String) As Boolean
    On Error Resume Next
    FileExists = (GetAttr(sFile) And vbDirectory) <> vbDirectory
    End Function

    Public Function PathExists(ByRef sPath As String) As Boolean
    On Error Resume Next
    PathExists = (GetAttr(sPath) And vbDirectory) = vbDirectory
    End Function
    You may also want to check out the URLDownloadToFile API function. Search this forum for examples of using it.
    Thank you. I'll try that. And thanks for the API, I'll look into it.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Error not being caught (RTE 76)

    Quote Originally Posted by abhi2011 View Post
    The error isn't being handled so I don't see how commenting out the if statement will have any effect. (I went line by line so I am pretty sure the label isn't being reached.)
    Your original post isn't clear at all. The way I interpreted, you're getting error but not not the one you expected.
    Whether or label was reached isn't relevant... Perhaps you learn from Bonnie's code sample how to code better (or cleaner if you will).

    Good luck.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Error not being caught (RTE 76)

    Just for future reference perhaps, there's a useful API that can be used to 'automatically' create a path if one does not exist
    Code:
    Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
                    (ByVal lpPath As String) As Long
    It saves having to iterate through a path creating each sub-directory if they don't already exist. For example:

    If I had a path such as 'C:\Doogle\MyDir\MyProject\Test' and only 'C:\Doogle' existed, executing

    Code:
    lngReturn = MakeSureDirectoryPathExists("C:\Doogle\MyDir\MyProject\Test\")
    will create all the sub-directories required. There's a limitation that the Function does not support Unicode strings.
    Last edited by Doogle; Jun 20th, 2013 at 03:46 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width