PDA

View Full Version : Solved: HTTP File Download



ChloeRadshaw
06-17-2009, 03:50 AM
Does anyone know how to download a file using VBA ?

I saw this
http://209.85.229.132/search?q=cache:iXaMgJuI5REJ:officeone.mvps.org/vba/http_download_file.html+vba+copy+file+from+url&cd=3&hl=en&ct=clnk&client=safari


But when I used the Inet object - It complained that the ActiveX control could not be enabled

Any ideas?

Oorang
06-17-2009, 11:46 PM
Hi Chloe,
I see the ActiveX saga continues:) Try this:

Option Explicit
Private Declare Function URLDownloadToFileA Lib "urlmon" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Public Sub Example1()
DownloadFile "http://www.google.com", "c:\Test\goog.txt"
Shell "notepad.exe ""c:\Test\goog.txt""", vbMaximizedFocus
End Sub

Private Function DownloadFile(URL As String, LocalFilename As String) As Boolean
'Thanks Mentalis:)
Dim lngRetVal As Long
lngRetVal = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function