none
VBA excel error(Unable to download the file) when using URLDownloadToFile RRS feed

  • Question

  • Hey guys I need to download image from image url in an excel workbook. Can some one please help me by looking at the code and let me know what i am doing wrong? when I run the macro I get error: Unable to download the file. Thanks alot: below is the vba code that iam trying to use:

    Option Explicit

    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _

    Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, _

    ByVal szURL As String, ByVal szFileName As String, _

    ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As LongPtr

    Dim Ret As LongPtr

    '~~> This is where the images will be saved. Change as applicable

    Const FolderName As String = "C:\\\\"

    Sub Sample()

        Dim ws As Worksheet

        Dim LastRow As Long, i As Long

        Dim strPath As String

        '~~> Name of the sheet which has the list

        Set ws = Sheets("Sheet1")

        LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

        For i = 2 To LastRow '<~~ 2 because row 1 has headers

            strPath = FolderName & ws.Range("A" & i).Value & ".jpg"

            Ret = URLDownloadToFile(0, ws.Range("B" & i).Value, strPath, 0, 0)

            If Ret = 0 Then

                ws.Range("C" & i).Value = "File successfully downloaded"

            Else

                ws.Range("C" & i).Value = "Unable to download the file"

            End If

        Next i

        End Sub

    Tuesday, October 18, 2016 11:48 AM

All replies

  • 4 forward slash given. I guess the function has "\" as resereved charecter but does probably C:\\ can do that to escape the character.

    Best Regards,
    Asadulla Javed,
    Jadavpore & Asansol

    Tuesday, October 18, 2016 2:52 PM
    Answerer
  • Hi,

    Could you manually download files from ws.Range("B" & i).Value?

    Do you check if you are using the correct strPath?

    You could see the similar thread: Problem with URLDownloadToFile Function

    Regards,

    Celeste

    Wednesday, October 19, 2016 1:34 PM
    Moderator