2

I had the need to download large number of files from the internet and so i wrote an excel macro by referring the web.But now i wanna download some files from website which are password protected as well so how should i alter my code so that it checks for authentication and raises a prompt for username and password if it is required? Thanks in advance here's my vba code Sub DownloadFiles()

'Declaring the necessary variables.
Dim sh                  As Worksheet
Dim DownloadFolder      As String
Dim LastRow             As Long
Dim SpecialChar()       As String
Dim SpecialCharFound    As Double
Dim FilePath            As String
Dim i                   As Long
Dim j                   As Integer
Dim Result              As Long
Dim CountErrors         As Long
Dim Username            As String
Dim Password            As String
Dim TextFile            As Integer
Dim CFilePath           As String
Dim Credentials         As Variant

'Disable screen flickering.
Application.ScreenUpdating = False

'Set the worksheet object to the desired sheet.
Set sh = Sheets("Main")

'An array with special characters that cannot be used for naming a file.
SpecialChar() = Split("\ / : * ? " & Chr$(34) & " < > |", " ")

'Find the last row.
 With sh
    .Activate
    LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With

'Check if the download folder exists.
DownloadFolder = sh.Range("B4")
On Error Resume Next
If Dir(DownloadFolder, vbDirectory) = vbNullString Then
    MsgBox "The folder's path is incorrect!", vbCritical, "Folder's Path Error"
    sh.Range("B4").Select
    Exit Sub
End If
On Error GoTo 0

'Check if there is at least one URL.
If LastRow < 8 Then
    MsgBox "You did't enter a single URL!", vbCritical, "No URL Error"
    sh.Range("C8").Select
    Exit Sub
End If

'Clear the results column.
sh.Range("E8:E" & LastRow).ClearContents

'Add the backslash if doesn't exist.
If Right(DownloadFolder, 1) <> "\" Then
    DownloadFolder = DownloadFolder & "\"
End If

'Counting the number of files that will not be downloaded.
CountErrors = 0

'Save the internet files at the specified folder of your hard disk.
On Error Resume Next
For i = 8 To LastRow

    'Use the given file name.
    If Not sh.Cells(i, 4) = vbNullString Then

        'Get the given file name.
        FilePath = sh.Cells(i, 4)

        'Check if the file path contains a special/illegal character.
        For j = LBound(SpecialChar) To UBound(SpecialChar)
            SpecialCharFound = InStr(1, FilePath, SpecialChar(j), vbTextCompare)
            'If an illegal character is found substitute it with a "-" character.
            If SpecialCharFound > 0 Then
                FilePath = WorksheetFunction.Substitute(FilePath, SpecialChar(j), "-")
            End If
        Next j

        'Create the final file path.
        FilePath = DownloadFolder & FilePath

        'Check if the file path exceeds the maximum allowable characters.
        If Len(FilePath) > 255 Then
            sh.Cells(i, 5) = "ERROR"
            CountErrors = CountErrors + 1
        End If

    Else
        'Empty file name.
        sh.Cells(i, 5) = "ERROR"
        CountErrors = CountErrors + 1
    End If

    'If the file path is valid, save the file into the selected folder.
    If UCase(sh.Cells(i, 5)) <> "ERROR" Then

        'Try to download and save the file.
        Result = URLDownloadToFile(0, sh.Cells(i, 3), FilePath, 0, 0)
        'Check if the file downloaded successfully and exists.
        If Result = 0 And Not Dir(FilePath, vbDirectory) = vbNullString Then
            'Success!
            sh.Cells(i, 5) = "OK"
        Else
            'Error!
            sh.Cells(i, 5) = "ERROR"
            CountErrors = CountErrors + 1

    End If
    End If

Next i
On Error GoTo 0

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that macro finished successfully or with errors.
If CountErrors = 0 Then
    'Success!
    If LastRow - 7 = 1 Then
        MsgBox "The file was successfully downloaded!", vbInformation, "Done"
    Else
        MsgBox LastRow - 7 & " files were successfully downloaded!", vbInformation, "Done"
    End If
Else
    'Error!
    If CountErrors = 1 Then
        MsgBox "There was an error with one of the files!", vbCritical, "Error"
    Else
        MsgBox "There was an error with " & CountErrors & " files!", vbCritical, "Error"
    End If
End If

End Sub

Ralph
  • 9,284
  • 4
  • 32
  • 42
Avi
  • 23
  • 4
  • Can yo also share the code for sub URLDownloadToFile(0, sh.Cells(i, 3), FilePath, 0, 0) – skkakkar Jul 06 '16 at 06:49
  • you can find it here http://stackoverflow.com/questions/26186279/urldownloadtofile-in-access-2010-sub-or-function-not-defined – Avi Jul 06 '16 at 12:17
  • Any specific reason for preferring this method over login using Winhttprequest. It seems that method is more convenient for login to password protected sites. – skkakkar Jul 06 '16 at 14:35
  • am not familiar with that method can u pls gimme an example?? – Avi Jul 11 '16 at 05:00
  • Please look into [Sub SaveFileFromURL](https://www.dropbox.com/s/xcx5utr7jfafqvb/sub%20savefilefromurl.docx?dl=0) for guidance. – skkakkar Jul 11 '16 at 05:29
  • okay thanks skkakkar – Avi Jul 11 '16 at 07:04

0 Answers0