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.
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...
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.
Re: Error not being caught (RTE 76)
Quote:
Originally Posted by
RhinoBull
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.)
Quote:
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.
Re: Error not being caught (RTE 76)
Quote:
Originally Posted by
abhi2011
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.
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.