Hello abhay_547,
Thought I recognized that code. I changed the download macro to Function to allow the URL and destination folder to be passed into the download loop. CommandButton5 now loops through URL and will display an error message if not successful. Also, a check is made that the destination folder has been selected. CommandButton2 has been updated to remove entries. All these changes are in the attached workbook.
Module1 - Amended Download Macro
Private Declare Function URLDownloadToFile _
Lib "urlmon.dll" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Function DownloadFilefromWeb(ByVal URL As String, ByVal SavePath As String) As Variant
Const E_OUTOFMEMORY As Long = &H8007000E
Const E_DOWNLOAD_FAILURE As Long = &H800C0002
Const E_INVALID_LINK As Long = &H800C000D
Dim InitialName As String
Dim Msg As String
Dim RegExp As Object
Dim RetVal As Long
If URL = "" Or SavePath = "" Then Exit Function
Set RegExp = CreateObject("VBScript.RegExp")
RegExp.IgnoreCase = True
RegExp.Pattern = "^(.*\/)(.+)$"
InitialName = RegExp.Replace(URL, "$2")
Set RegExp = Nothing
If InitialName = "" Or InitialName = URL Then
MsgBox "Error - Missing File Name"
Exit Function
End If
RetVal = URLDownloadToFile(0&, URL, SavePath, 0&, 0&)
Select Case RetVal
Case 0
DownloadFilefromWeb = True
Case E_OUTOFMEMORY
DownloadFilefromWeb = URL & vbCrLf & "Error - Out of Memory"
Case E_DOWNLOAD_FAILURE
DownloadFilefromWeb = URL & vbCrLf & "Error - Bad URL or Connection Interrupted"
Case E_INVALID_LINK
DownloadFilefromWeb = URL & vbCrLf & "Error - Invalid Link or Protocol Not Supported"
Case Else
DownloadFilefromWeb = URL & vbCrLf & "Error - Unknown = " & Hex(RetVal)
End Select
End Function
frmdownload CommandButton2 and CommandButton5 Macro Code
Private Sub CommandButton2_Click()
'REMOVE SELECTION
Dim I As Long
With ListBox1
For I = .ListCount - 1 To 0 Step -1
If .Selected(I) Then
.RemoveItem .ListIndex
End If
Next I
End With
End Sub
Private Sub CommandButton5_Click()
'DOWNLOAD FILE
Dim I As Integer
Dim Ret As Variant
If TextBox1.Value = "" Then MsgBox "Please select a folder.": Exit Sub
For I = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(I) Then
Ret = DownloadFilefromWeb(ListBox1.List(I), TextBox1.Value)
If VarType(Ret) <> vbBoolean Then
MsgBox Ret
End If
End If
Next I
End Sub
Bookmarks