Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:01:03+00:00 2026-06-04T15:01:03+00:00

I can successfully download files from server using the macro below, but sometimes the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:01:04+00:00Added an answer on June 4, 2026 at 3:01 pm

    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

    enter image description here

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASIHTTPRequest for downloading file from server but its giving error Failed
I am using Chilkat FTP component to download some files from an FTP Server
I am trying to download files from an ftp server using C# and ftpwebrequest.
I am attempting to download a gunzipped file from an FTP server. It appears
I trying to get a file from an FTP server using ftp_get from PHP
right now i am able to download remote files successfully, but this is only
i have a simple http client server. here client can download from the server.
We have a code to download a PDF from the server using a SOAP
i have an iphone 3g and can successfully send text messages using the PHP
I'm creating page where users can upload/download pdf files which is stored in mysql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.