Download Image from a URL with rename issue

Calltech

New Member
Joined
Dec 4, 2015
Messages
19
Hello!

I would like to ask for help to a macro issue.

I found a solution to: Download Image from B column to specific path with renamed by A column. Add text to C, if it's successful or not.



Before macro:

PIC NAMEURL
350x150https://placeholdit.imgix.net/~text?
txtsize=33&txt=350%C3%97150&w=350&h=150
300x200https://placeholdit.imgix.net/~text?
txtsize=28&txt=300%C3%97200&w=300&h=200
......

<tbody>
</tbody>



After macro:

PIC NAMEURLStatus
350x150https://placeholdit.imgix.net/~text?
txtsize=33&txt=350%C3%97150&w=350&h=150
File successfully downloaded
300x200https://placeholdit.imgix.net/~text?
txtsize=28&txt=300%C3%97200&w=300&h=200
File successfully downloaded
.........

<tbody>
</tbody>

8lnldsq




Here is the macro:

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Option Explicit

PrivateDeclareFunction URLDownloadToFile Lib"urlmon" _
Alias"URLDownloadToFileA"(ByVal pCaller AsLong, _
ByVal szURL AsString,ByVal szFileName AsString, _
ByVal dwReserved AsLong,ByVal lpfnCB AsLong)AsLong

Dim Ret AsLong

'~~> This is where the images will be saved. Change as applicable
Const FolderName AsString="C:\Temp"

Sub Sample()
Dim ws As Worksheet
Dim LastRow AsLong, i AsLong
Dim strPath AsString

'~~> Name of the sheet which has the list
Set ws = Sheets("Sheet1")

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

For i =2To 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 =0Then
ws
.Range("C"& i).Value ="File successfully downloaded"
Else
ws
.Range("C"& i).Value ="Unable to download the file"
EndIf
Next i
EndSub

</code>

Many people has error with this macro:


Compile error: The code in this project must be updated for use on64-bit systems.




The solution if change the macro codes to it:

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Option Explicit

PrivateDeclareFunction URLDownloadToFile Lib"urlmon" _
Alias"URLDownloadToFileA"(ByVal pCaller AsLong, _
ByVal szURL AsString,ByVal szFileName AsString, _
ByVal dwReserved AsLong,ByVal lpfnCB AsLong)AsLong

Dim Ret AsLong

'~~> This is where the images will be saved. Change as applicable
Const FolderName AsString="C:\Temp"



</code>But I still have a continue error: :(

oono9u0




I would like to ask for help why can't working this macro for me?
I tried to change the URLDownloadToFile code row to others still unsuccessfully.

Thank you all in advance for your help!

Calltech
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
The code you've posted is missing a lot of spaces which make it incorrect: PrivateDeclareFunction, AsLong and AsString - these have all lost space characters (Private Declare Function, As Long and As String).

The code you're actually running, does that have spaces in all the correct places or is it exactly the same as you've posted?
 
Upvote 0
This works for me:-
Code:
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

Dim Ret As Long

[COLOR=#008000]'~~> This is where the images will be saved. Change as applicable
[/COLOR]Const FolderName As String = "C:\Temp\"

Sub Sample()

  Dim ws As Worksheet
  Dim LastRow As Long, i As Long
  Dim strPath As String
  
  '~[COLOR=#008000]~> Name of the sheet which has the list
[/COLOR]  Set ws = Sheets("Sheet1")
  
  LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
  
  For i = 2 To LastRow '<~[COLOR=#008000]~ 2 because row 1 has headers[/COLOR]
    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

(I had to add the trailing backslash in FolderName.)
 
Upvote 0
And I would maybe add this line of code to clear your results column immediately after LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row:-
Code:
  ws.Range("C2:C" & LastRow).ClearContents
 
Upvote 0
Sorry I copied a bad changes.

Option Explicit


Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByRef pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserve As Long, _
ByRef lpfnCB As LongPtr) _
As LongPtr


Dim Ret As Long


'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "C:\Temp"


The original code working with Microsoft Office 2010 x64bit. So this must be change to who has a Microsoft Office 2010 x32bit.

I use Microsoft Office Professional Plus 2013 64bit, but I tried with Microsoft Office 2010 x32bit and not worked.
 
Upvote 0
Yes it's look like! -> Const FolderName As String = "C:\Temp"
But in the forum must add \\ after Temp to show -> Const FolderName As String = "C:\Temp\"


During I fixed the links with MicrosoftEasyFix50655.msi
Now if I click the link it's loading on browser, but the problem not solved.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top