I can successfully download files from server using the macro below, but sometimes the file name may be incorrect so that I have to manually go through the directory and compare what was downloaded versus what should have been downloaded which has become very time consuming. What do I need to include in this macro to make it provide of log of what was not downloaded?
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
Sub imagedownloader()
Dim i As Long, url As String
With ActiveWorkbook.Sheets("Sheet1") 'must use the name of the sheet to do this
For i = 1 To 4 'Where 4 is the number of items in the list (can be made dynamic)
DoEvents
url = "http://mydomain.com/images/" & .Range("A" & i).Value & ".jpg"
URLDownloadToFile 0, url, "C:\downloads\images\" & .Range("A" & i).Value & ".jpg", 0, 0
Next
End With
End Sub
Is this what you are trying? (UNTESTED)
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
Sub imagedownloader()
Dim i As Long
Dim filesize As Integer
Dim FlName As String, url As String
FlName = "C:\downloads\images\Log.Txt"
'~~> get a free file handle
filesize = FreeFile()
'~~> Open your file
Open FlName For Output As #filesize
With ActiveWorkbook.Sheets("Sheet1")
For i = 1 To 4
url = "http://mydomain.com/images/" & .Range("A" & i).Value & ".jpg"
URLDownloadToFile 0, url, "C:\downloads\images\" & .Range("A" & i).Value & ".jpg", 0, 0
DoEvents
If DoesFileExist("C:\downloads\images\" & .Range("A" & i).Value & ".jpg") Then
Print #filesize, .Range("A" & i).Value & ".jpg - Successfully Downloaded"
Else
Print #filesize, .Range("A" & i).Value & ".jpg - Not Downloaded"
End If
Next
End With
Close #filesize
End Sub
Public Function DoesFileExist(FilePath As String) As Boolean
On Error GoTo Whoa
If Not Dir(FilePath, vbDirectory) = vbNullString Then DoesFileExist = True
Whoa:
On Error GoTo 0
End Function
FOLLOWUP
Here is an advanced version (TESTED AND TRIED). This is required because URLDownloadToFile
will download a file even if it is not available. The only thing in this case would be that the image file would be corrupt.
The best way to handle this is create a userform and add an image control. You can set the image control’s visible property to false if you want. Now use this code. I have commented it so you will have not problems in understanding it 🙂
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 Sub CommandButton1_Click()
Dim i As Long
Dim filesize As Integer
Dim FlName As String, url As String
FlName = "C:\downloads\images\Log.Txt"
'~~> Get a free file handle
filesize = FreeFile()
'~~> Open your file
Open FlName For Output As #filesize
With ActiveWorkbook.Sheets("Sheet1")
For i = 1 To 4
url = "http://capnhud.host22.com/examples/" & .Range("A" & i).Value & ".jpg"
URLDownloadToFile 0, url, "C:\downloads\images\" & .Range("A" & i).Value & ".jpg", 0, 0
DoEvents
'~~> Try to load the downloaded image to Image1 Control
On Error Resume Next
Set Image1.Picture = LoadPicture("C:\downloads\images\" & .Range("A" & i).Value & ".jpg")
'~~> If image is not in the correct format then delete it
If Not Err.Number = 0 Then
Kill "C:\downloads\images\" & .Range("A" & i).Value & ".jpg"
Print #filesize, .Range("A" & i).Value & ".jpg - Not Downloaded"
Else
Print #filesize, .Range("A" & i).Value & ".jpg - Successfully Downloaded"
End If
Next
End With
Close #filesize
End Sub
SNAPSHOT
NOTE: You now actually don’t need DoesFileExist
function. You can do the writing to the text in If Not Err.Number = 0 Then
as well.
SAMPLE FILE
http://wikisend.com/download/310908/Sample.xlsm
HTH
Tags: excelexcel, file, logging, vba