Hi,Hi,
I am in search of vba macro with which I can download files from hyperlinks listed in B column and save each in respective new folder (listed in column A).
Any help will be appreciated.
Thanks,
Samadhan
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
'~~> This is where the images will be saved. Change as applicable
Const ParentFolderName As String = "C:\Users\a3rgcw\Downloads\"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim Folderpath, strPath As String
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
Folderpath = ParentFolderName & ws.Range("A" & i).Value & "\"
If Len(Dir(Folderpath, vbDirectory)) = 0 Then
MkDir Folderpath
End If
strPath = Folderpath & "File" & i & ".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
Hi,
Thank you for providing such a nice code.
It is working fine as expected.
But files that I am downloading are .7z or .zip files.
Macro is creating folders to dump these files in respective folder but all are almost empty or in some cases there is File (unwanted or may be log file) created.
Note that there is no direct path to file in hyperlink. I have added in uploaded .xlsm file.
Don't mention it, just glad I could helpYeah...issue resolved after removing '*'.
Actually if go for manual download, user has to click button of that file and java script does download data. I had to automate that thing.
Recorded manual script to create hyperlink from source code and at next your script is helping me ahead!
Thank you so much....!!!!
Option Explicit
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
'~~> This is where the images will be saved. Change as applicable
Const ParentFolderName As String = "\\gssux002\GBS_CHE_GL_TMS\ICV\"
Sub Download_Report()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim Folderpath, strPath As String
Set ws = Sheets("ICV Report")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Folderpath = ParentFolderName & ws.Range("A" & i).Value & "\"
If Len(Dir(Folderpath, vbDirectory)) = 0 Then
MkDir Folderpath
End If
strPath = Folderpath & ws.Range("A" & i).Value & i & ".*"
Ret = URLDownloadToFile(0, ws.Range("H" & i).Value, strPath, 0, 0)
If Ret = 0 Then
ws.Range("I" & i).Value = "File successfully downloaded"
Else
ws.Range("I" & i).Value = "Unable to download the file"
End If
Next i
MsgBox "Completed This process !!!", vbInformation
End Sub