Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

downloading files from a website 2

Status
Not open for further replies.

BitZero

Programmer
Mar 11, 2008
100
US
I periodically download (large) zip files from a website, and I would like to automate the process in VBA. The website url is of the form and then I click on a link that displays a File Download window with Open, Save and Cancel buttons. I click on Save and save it to my C: drive. (When I hover over the link, it says the file name is of the form
I have added references for "Microsoft Internet Controls" and "Microsoft HTML Object Library", which let me create InternetExplorer and HTMLDocument objects. But I've never used these objects before and I don't know how to put the pieces together.

I'm using Access 2003.

Any help would be appreciated. Thanks
 
btw in the above sub it attempts to set the Text box value like this: "Text1.Text = "" change to Text1.Value = "" or access will tell you the control needs focus

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Here's a really quick and dirty VBA function to download a url to a file.
Code:
Public Function Wget(sSourceUrl As String, sDestinationPath As String) As Boolean
      'VBA function version of Wget.
      'It takes a url (sSourceUrl) and downloads the URL to sDestinationPath.

10        On Error GoTo Wget_Error

20        With New WinHttpRequest
              'Open a request to our source
30            .Open "GET", sSourceUrl

              'I need to set this to get it to go through the firewall
40            .SetAutoLogonPolicy AutoLogonPolicy_Always
50            .SetProxy 2, "[URL unfurl="true"]http://127.0.0.1:8888",[/URL] "*.never"
60            .SetRequestHeader "Accept", "*/*"

              'Set any options you may need [URL unfurl="true"]http://msdn.microsoft.com/en-us/library/windows/desktop/aa384108(v=vs.85).aspx[/URL]
              'Set a custom useragent, not needed, but could be useful if there are problems on the server
70            .Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; VBA Wget)"

              'Automatically follow any redirects
80            .Option(WinHttpRequestOption_EnableRedirects) = "True"

90            .Send

              'You could check .Status here to ensure you've received a proper repsonse
              'You will also need to decide what to do if sDestinationPath exists. Here it just overwrites it

              'Write the responseBody to a file
              Dim ado As New ADODB.Stream
100           ado.Type = adTypeBinary
110           ado.Open
120           ado.Write .ResponseBody
130           ado.SaveToFile sDestinationPath, adSaveCreateOverWrite
140           ado.Close

150       End With

160       Wget = True    'download was successful
Wget_Exit:
170       On Error Resume Next
180       Set ado = Nothing
190       Exit Function

Wget_Error:
200       Wget = False    'An error occurred
210       Select Case Err
          Case Else
220           Debug.Print "Unhandled Error in Wget", Err.Number, Err.Description, Err.Source, Erl()
230       End Select
240       Resume Wget_Exit
250       Resume
End Function

To use it, you will need to add references to Microsoft WinHTTP Services and Microsoft ActiveX Data Object 2.x Library

Code:
Public Sub Test()
    Wget "[URL unfurl="true"]http://www.tek-tips.com/images/header-logo.gif",[/URL] "C:\TEMP\header-logo.gif"
End Sub

hth

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top