Operation Aborted error from URLDownLoadToFile

lr185021

New member
Local time
Today, 11:19
Joined
May 15, 2012
Messages
2
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
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:
Re: Working URL (WebCam) Capture to File Progrma

I'm truly disappointed that not a sole offered a shred of assistance on this problem. Fortunately, I believe that where there is a will, there is a way and FINALLY, 24 hours later, I fought my way through and made it work. Hopefully this will save someone else all the time it cost me. The code is fully commented. It was written to capture a Cruise Ship webcam stream at 1 minute intervals, starting at a specified time. URL, Start time and interval can be easily changed in the parameters section of the code. I had to remove the URL in order to post since I'm a newbie with less than 10 posts.

Now a Very Happy Camper in NC

Code:
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 DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
    
Private Const BINDF_GETNEWESTVERSION As Long = &H10

Public Sub TimeLapseDownloadURLtoFile()
'This code captures a webcam stream at prescribed intervals and saves it to a unique timestamped file name
'Code is intentionally looped to run until stopped via Ctrl-Break

Dim returnvalue As Long, ts As String, interval As Integer, starttime As String
Set ie = CreateObject("InternetExplorer.Application")
ts = 0

'Set runtime parameters
interval = 1 'set the interval for the capture - currently set at 1 minute
starttime = "201206060400" 'set start time for capture in the format yyyymmddhhmm
URL = "...your URL goes here..." 'url to capture from

Do While ts < starttime 'wait until the desired time to start the capture
    ts = Format(Date, "yyyymmdd") & Format(Time, "hhmm") 'rest timestamp
Loop

 Do While True
   FileName = Format(Date, "yyyymmdd") & Format(Time, "hhmm") & ".jpg" 'build filename from timestamp
   DeleteUrlCacheEntry (URL) 'Delete Cached page to get a fresh one
   returnvalue = URLDownloadToFile(0, URL, FileName, BINDF_GETNEWESTVERSION, 0&) 'capture the file
   If returnvalue = 0 Then 'check returnvalye from file capture
       TWait = Time
       TWait = DateAdd("n", interval, TWait) ' adjust delay minutes by changing value of variable interval
       'Interval type is n(minutes) but could be changed to s(seconds), h(hours), d(days),
       Do Until TNow >= TWait 'execute delay loop
           TNow = Time
       Loop
  Else 'abort if a bad returnvalue is generated from file capture
      MsgBox ("Download unsuccessful - return value " & returnvalue & vbCancel)
      Exit Sub
  End If
Loop
 
End Sub

Public Function RunWebCamCapture()
    TimeLapseDownloadURLtoFile
End Function
 
Last edited by a moderator:

Users who are viewing this thread

Back
Top Bottom