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)
FOLLOWUP
Here is an advanced version (TESTED AND TRIED). This is required because
URLDownloadToFilewill 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 🙂
SNAPSHOT
NOTE: You now actually don’t need
DoesFileExistfunction. You can do the writing to the text inIf Not Err.Number = 0 Thenas well.SAMPLE FILE
http://wikisend.com/download/310908/Sample.xlsm
HTH