Advertisement
Guest User

Untitled

a guest
Sep 10th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. 'API function declaration for both 32 and 64bit Excel.
  4. #If VBA7 Then
  5.     Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
  6.                                     (ByVal pCaller As Long, _
  7.                                     ByVal szURL As String, _
  8.                                     ByVal szFileName As String, _
  9.                                     ByVal dwReserved As Long, _
  10.                                     ByVal lpfnCB As Long) As Long
  11. #Else
  12.     Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
  13.                             (ByVal pCaller As Long, _
  14.                             ByVal szURL As String, _
  15.                             ByVal szFileName As String, _
  16.                             ByVal dwReserved As Long, _
  17.                             ByVal lpfnCB As Long) As Long
  18. #End If
  19.  
  20. Sub DownloadFiles()
  21.                    
  22.     '--------------------------------------------------------------------------------------------------
  23.    'The macro loops through all the URLs (column C) and downloads the files at the specified folder.
  24.    'The given file names (column D) are used to create the full path of the files.
  25.    'If the file is downloaded successfully an OK will appear in column E (otherwise an ERROR value).
  26.    'The code is based on API function URLDownloadToFile, which actually does all the work.
  27.            
  28.     'Written By:    Christos Samaras
  29.    'Date:          28/05/2014
  30.    'Last Update:   06/06/2015
  31.    'E-mail:        xristos.samaras@gmail.com
  32.    'Site:          http://www.myengineeringworld.net
  33.    '--------------------------------------------------------------------------------------------------
  34.    
  35.     'Declaring the necessary variables.
  36.    Dim sh                  As Worksheet
  37.     Dim DownloadFolder      As String
  38.     Dim LastRow             As Long
  39.     Dim SpecialChar()       As String
  40.     Dim SpecialCharFound    As Double
  41.     Dim FilePath            As String
  42.     Dim i                   As Long
  43.     Dim j                   As Integer
  44.     Dim Result              As Long
  45.     Dim CountErrors         As Long
  46.     Dim ErrMsg              As String
  47.    
  48.     'Disable screen flickering.
  49.    Application.ScreenUpdating = False
  50.    
  51.     'Set the worksheet object to the desired sheet.
  52.    Set sh = Sheets("Main")
  53.    
  54.     'An array with special characters that cannot be used for naming a file.
  55.    SpecialChar() = Split("\ / : * ? " & Chr$(34) & " < > |", " ")
  56.    
  57.     'Find the last row.
  58.     With sh
  59.         .Activate
  60.         LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
  61.     End With
  62.    
  63.     'Check if the download folder exists.
  64.    DownloadFolder = sh.Range("B4")
  65.  
  66.     On Error Resume Next
  67.     If Dir(DownloadFolder, vbDirectory) = vbNullString Then
  68.         MsgBox "The folder's path is incorrect!", vbCritical, "Folder's Path Error"
  69.         sh.Range("B4").Select
  70.         Exit Sub
  71.     End If
  72.     On Error GoTo 0
  73.                
  74.     'Check if there is at least one URL.
  75.    If LastRow < 8 Then
  76.         MsgBox "You did't enter a single URL!", vbCritical, "No URL Error"
  77.         sh.Range("C8").Select
  78.         Exit Sub
  79.     End If
  80.    
  81.     'Clear the results column.
  82.    sh.Range("E8:E" & LastRow).ClearContents
  83.    
  84.     'Add the backslash if doesn't exist.
  85.    If Right(DownloadFolder, 1) <> "\" Then
  86.         DownloadFolder = DownloadFolder & "\"
  87.     End If
  88.  
  89.     'Counting the number of files that will not be downloaded.
  90.    CountErrors = 0
  91.    
  92.     'Save the internet files at the specified folder of your hard disk.
  93.    On Error GoTo ErrorHandler
  94.     For i = 8 To LastRow
  95.    
  96.         'Use the given file name.
  97.        If Not sh.Cells(i, 4) = vbNullString Then
  98.            
  99.             'Get the given file name.
  100.            FilePath = sh.Cells(i, 4)
  101.            
  102.             'Check if the file path contains a special/illegal character.
  103.            For j = LBound(SpecialChar) To UBound(SpecialChar)
  104.                 SpecialCharFound = InStr(1, FilePath, SpecialChar(j), vbTextCompare)
  105.                 'If an illegal character is found substitute it with a "-" character.
  106.                If SpecialCharFound > 0 Then
  107.                     FilePath = WorksheetFunction.Substitute(FilePath, SpecialChar(j), "-")
  108.                 End If
  109.             Next j
  110.            
  111.             'Create the final file path.
  112.            FilePath = DownloadFolder & FilePath
  113.            
  114.             'Check if the file path exceeds the maximum allowable characters.
  115.            If Len(FilePath) > 255 Then
  116.                 sh.Cells(i, 5) = "ERROR: Filename too long"
  117.                 CountErrors = CountErrors + 1
  118.             End If
  119.                
  120.         Else
  121.             'Empty file name.
  122.            sh.Cells(i, 5) = "ERROR: Empty file name"
  123.             CountErrors = CountErrors + 1
  124.         End If
  125.        
  126.         'If the file path is valid, save the file into the selected folder.
  127.        If UCase(sh.Cells(i, 5)) <> "ERROR" Then
  128.        
  129.             'Try to download and save the file.
  130.            Result = URLDownloadToFile(0, Scrape(sh.Cells(i, 3)), FilePath, 0, 0)
  131.            
  132.             'Check if the file downloaded successfully and exists.
  133.            If Result = 0 And Not Dir(FilePath, vbDirectory) = vbNullString Then
  134.                 'Success!
  135.                sh.Cells(i, 5) = "OK"
  136.             Else
  137.                 'Error!
  138.                sh.Cells(i, 5) = "ERROR ON DOWNLOAD"
  139.                 CountErrors = CountErrors + 1
  140.             End If
  141.            
  142.         End If
  143.  
  144.  ErrorHandler:
  145.     If Err.Number <> 0 Then
  146.         ErrMsg = "Error n. " & Str(Err.Number) & " was generated by " _
  147.          & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
  148.         MsgBox ErrMsg, , "Error", Err.HelpFile, Err.HelpContext
  149.     End If
  150.     Resume Next
  151.        
  152.     Next i
  153.     On Error GoTo 0
  154.    
  155.     'Enable the screen.
  156.    Application.ScreenUpdating = True
  157.    
  158.     'Inform the user that macro finished successfully or with errors.
  159.    If CountErrors = 0 Then
  160.         'Success!
  161.        If LastRow - 7 = 1 Then
  162.             MsgBox "The file was successfully downloaded!", vbInformation, "Done"
  163.         Else
  164.             MsgBox LastRow - 7 & " files were successfully downloaded!", vbInformation, "Done"
  165.         End If
  166.     Else
  167.         'Error!
  168.        If CountErrors = 1 Then
  169.             MsgBox "There was an error with one of the files!", vbCritical, "Error"
  170.         Else
  171.             MsgBox "There was an error with " & CountErrors & " files!", vbCritical, "Error"
  172.         End If
  173.     End If
  174.    
  175. End Sub
  176.  
  177. Function Scrape(URL As String) As String
  178.   Dim Browser As Object
  179.   Dim Document As Object
  180.   Dim ReceiptElement as Object
  181.   Dim ButtonElement as Object
  182.  
  183.   Set Browser = CreateObject("InternetExplorer.Application")
  184.   MsgBox "IE object set ", vbInformation, "Info"
  185.    
  186.   Browser.Visible = False
  187.   MsgBox "Options set; init navigation", vbInformation, "Info"
  188.   Browser.navigate URL
  189.   MsgBox "Navigation set, loading page", vbInformation, "Info"
  190.  
  191.   Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
  192.       DoEvents
  193.   Loop
  194.   MsgBox "Loading complete, init scrape ", vbInformation, "Info"
  195.   Set Document = Browser.Document
  196.   Set ReceiptElement = Document.getElementById("receipt")
  197.   Set ButtonElement = ReceiptElement.getElementByTagName("a")
  198.   Scrape = ButtonElement.href
  199.   MsgBox "URL scraped: " & Scrape & " ", vbInformation, "Info"
  200. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement