In my VBA Module under Access 2003 on Windows7, I'm trying to execute URLDownLoadToFile and I'm getting a -2147467260 return code on the function call to URLDownLoadToFile which appears to be 'Operation Aborted'. I am Lan connected to the Internet and can view the same URL in another window so I know it's valid. The directory exists into which I am writing. I turned off my firewall just in case that was part of the problem but got the same result.
I'm not aborting it -- any ideas why I might get this one? I've ransacked the web and can't find a reason... Can anyone offer me anything else to try?
Thanks,
Pulling Hair out in NC
I'm not aborting it -- any ideas why I might get this one? I've ransacked the web and can't find a reason... Can anyone offer me anything else to try?
Thanks,
Pulling Hair out in NC
Code:
Option Explicit
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
Private Const INTERNET_CONNECTION_CONFIGURED = &H40
Private Const INTERNET_CONNECTION_LAN = &H2
Private Const INTERNET_CONNECTION_MODEM = &H1
Private Const INTERNET_CONNECTION_OFFLINE = &H20
Private Const INTERNET_CONNECTION_PROXY = &H4
Private Const INTERNET_RAS_INSTALLED = &H10
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long
'Purpose : Retreview text from a web site
'Inputs : sURLFileName The URL and file name to download.
' sSaveToFile The filename to save the file to.
' [bOverwriteExisting] If True overwrites the file if it existings
'Outputs : Returns True on success.
Function InternetGetFile(sURLFileName As String, sSaveToFile As String, Optional bOverwriteExisting As Boolean = False) As Boolean
Dim lRet As Long, ret As Long
Const S_OK As Long = 0, E_OUTOFMEMORY = &H8007000E
Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000
Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_FLAG_RELOAD = &H80000000
On Error Resume Next
'Create an internet connection
lRet = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
'show the result
InternetGetConnectedState ret, 0&
If (ret And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED Then MsgBox "Local system has a valid connection to the Internet, but it may or may not be currently connected."
If (ret And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN Then MsgBox "Local system uses a local area network to connect to the Internet."
If (ret And INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM Then MsgBox "Local system uses a modem to connect to the Internet."
If (ret And INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE Then MsgBox "Local system is in offline mode."
If (ret And INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY Then MsgBox "Local system uses a proxy server to connect to the Internet."
If (ret And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then MsgBox "Local system has RAS installed."
If bOverwriteExisting Then
If Len(Dir$(sSaveToFile)) Then
VBA.Kill sSaveToFile
End If
End If
'Check file doesn't already exist
If Len(Dir$(sSaveToFile)) = 0 Then
'Download file
lRet = URLDownloadToFile(0, sURLFileName, sSaveToFile, 0, 0)
If Len(Dir$(sSaveToFile)) Then
'File successfully downloaded
InternetGetFile = True
Else
'Failed to download file
If lRet = E_OUTOFMEMORY Then
Debug.Print "The buffer length is invalid or there was insufficient memory to complete the operation."
Else
Debug.Assert False
Debug.Print "Error occurred " & lRet & " (this is probably a proxy server error)."
End If
InternetGetFile = False
End If
End If
On Error GoTo 0
End Function
'Demonstration routine, downloads an image to the c: drive
Sub Test()
Dim url As String, filename As String, filectr As Long
filectr = 0
filename = ...filename... 'had to remove actual url and filename to be able to post this but they are valid
url = ...url...
If InternetGetFile(url, filename, True) Then
MsgBox "Successfully downloaded file!"
Else
MsgBox "Failed to download file!"
End If
End Sub
Last edited by a moderator: