Excel macro/vba that will download url image to folder on desktop with file name

jbrig

New Member
Joined
Jun 8, 2017
Messages
2
[h=2][/h]I'm looking for help with a macro that will download a url image to a folder on the desktop and rename each image from another column. All image urls are in column B and the name for each would be in column A. Thanks to anyone that can help!!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This should do nicely:

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
Private Sub DownloadFile(strWebFilename As String, strSaveFileName As String)
' Download the file.
     URLDownloadToFile 0, strWebFilename, strSaveFileName, 0, 0
End Sub

Sub Demo()
    Dim oCell As Range
    Dim sPath As String
    sPath = "c:\users\yourUserName\desktop\FolderName\"
    For Each oCell In Intersect(ActiveSheet.UsedRange, ActiveSheet.Range("B:B"))
        DownloadFile oCell.Value, oCell.Offset(, 1).Value
    Next
End Sub
 
Upvote 0
I am very new to this so it might be something I did wrong. But I am getting this error in this section - Compile error: Only comments may appear after End Sub, End Function, or End Property

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
 
Upvote 0
The declaration must be done at the top of a module. The remainder of the code can go anywhere in a module.
 
Last edited:
Upvote 0
This function API don't work for me. It shows error about 64- bit computer. Is there any other solution except using this API..??
 
Upvote 0
Update the declaration to this:
Rich (BB code):
#If VBA7 Then
Private Declare PtrSafe 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
 #Else 
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
 #End  If
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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