Downloading all files with the prefix from the website in VBA - vba

I have a website where there are 100 links to csv files that are automatically downloaded after clicking. Each of the files has a prefix in the form aaa_.
The following standard code allows you to save a file based on the URL to the selected location on the disk:
Sub Download_from_website()
Dim myURL As String
myURL = "https://mysite/2500/csv/aaa_1.csv"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send
myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile ("C:\Users\tkp\Desktop\download_from_website\aaa_1.csv")
oStream.Close
End If
End Sub
How can you transform the above code so that you can automatically search the web page to find all links in which the string aaa_ appears and automatically save in the selected location? I will be very grateful for any tips.
The above code was a simplified example of what I would like to get. However, in fact I would like to save all the files that have the SEB_ prefix from the site
https://sebgroup.lu/private/luxembourg-based-funds/download-of-portfolio-holdings

This should manage them
Option Explicit
#If VBA7 And Win64 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr _
) 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
Public Const BINDF_GETNEWESTVERSION As Long = &H10
Public Sub GetLinks()
Dim sResponse As String, html As New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://sebgroup.lu/private/luxembourg-based-funds/download-of-portfolio-holdings", False
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
With html
.body.innerHTML = sResponse
Dim list As Object, i As Long
Set list = html.getElementsByClassName("linklist")(0).getElementsByTagName("a")
For i = 0 To list.Length - 1
If instr(list(i).getAttribute("href"),"SEB_") > 0 Then
downloadfile list(i).getAttribute("href")
End If
Next i
End With
End Sub
Public Sub downloadfile(ByVal url As String)
Dim fileName As String, fileNames() As String, folderName As String
fileNames = Split(url, "/")
fileName = fileNames(UBound(fileNames))
folderName = "C:\Users\User\Desktop\CurrentDownloads\" & fileName '<==change as required
Dim ret As Long
ret = URLDownloadToFile(0, url, folderName, BINDF_GETNEWESTVERSION, 0)
End Sub

Give the following script a go. I suppose it will fix the issues you are having now. I've written this script considering the fact that you want all the csv files which have seb in their links.
Here you go:
Sub DownloadFilesFromWeb()
Const URL As String = "https://sebgroup.lu/private/luxembourg-based-funds/download-of-portfolio-holdings"
Dim Http As New WinHttp.WinHttpRequest, Html As New HTMLDocument, I&, tempArr As Variant
With Http
.Open "GET", URL, False
.send
Html.body.innerHTML = .responseText
End With
With Html.querySelectorAll(".linklist a[href*='seb']")
For I = 0 To .Length - 1
tempArr = Split(.item(I).getAttribute("href"), "/")
tempArr = tempArr(UBound(tempArr))
Http.Open "GET", .item(I).getAttribute("href"), False
Http.send
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.write Http.responseBody
''notice the following line how the "tempArr" should be appended to the folder you have
.SaveToFile "C:\Users\WCS\Desktop\downloadfile\" & tempArr
.Close
End With
Next I
End With
End Sub
Reference to add to the library:
Microsoft HTML Object Library
Microsoft WinHTTP Services, version 5.1

Related

how to solve error when downloading images using vba?

I tried to download the image from the URL and it worked normally.
but if it is run on a 64 bit computer then an error message
"Compile error in hidden module: Module 10.
This error commonly occurs when code is incompatible with the version,platform"
I don't know what to do, any ideas?
this is my 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
Dim Ret As Long
Sub Download()
Dim strPath As String
Dim FolderName As String
Dim x As Integer
Dim i As Long
Dim sData As Worksheet: Set sData = Sheets("Sheet17")
Application.DisplayAlerts = False
FolderName = "C:\Try"
With sData
For i = 1 To 100
For x = 5 To 12
Application.Calculation = xlCalculationManual
If Sheet17.Cells(i, x).Value <> "" Then
strPath = FolderName & "\" & i & "-" & x - 4 & ".jpg"
Ret = URLDownloadToFile(0, Sheet17.Cells(i, x).Value, strPath, 0, 0)
End If
Next x
Next i
Application.Calculation = xlCalculationAutomatic
End With
Application.DisplayAlerts = True
End Sub

Downloading a PDF

I am trying to download a .pdf file which opens up in an IE Web browser. Here is my code:
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
Sub DownPDF()
Dim sUrl As String
Dim sPath As String
sPath = "C:\Users\adhil\Documents"
sUrl = "http://analytics.ncsu.edu/sesug/2010/HOW01.Waller.pdf"
Ret = URLDownloadToFile(0, sUrl, sPath, 0, 0)
If Ret = 0 Then
Debug.Print sUrl & " downloaded to " & sPath
Else
Debug.Print sUrl & " not downloaded"
End If
End Sub
However, I am unable to get the file downloaded although response says so.
Can anyone assist me?
The function expects the parameter szFileName to be a fullpath name of a file, not a folder.
Try with this:
sPath = "C:\Users\adhil\Documents\HOW01.Waller.pdf"
I worked with me, while it did not work when the destination file name was omitted.

File download in IE10

I'm working on an internet explorer automation project. The code downloads and saves a file from a website. Its possible to download and save file using 'SendKeys' but its not a reliable method as I cannot detect the download notification:
Is there a way to download and save the file without 'SendKeys'? or at least is there a way to detect the presence of this notification?
Ps - I have referred to these links, which are helpful for IE8 downloads:
http://www.siddharthrout.com/2011/10/23/vbavb-netvb6click-opensavecancel-button-on-ie-download-window/
http://www.siddharthrout.com/2012/02/02/vbavb-netvb6click-opensavecancel-button-on-ie-download-window-part-ii/
Any help?
Why to use SendKeys method to download a file? Sorry, this is wrong approach.
Use API!
Solution 1:
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
Sub DownloadFilefromWeb()
Dim strSavePath As String
Dim URL As String, ext As String
Dim buf, ret As Long
URL = Worksheets("References & Resources").Range("URLMSL").Value
buf = Split(URL, ".")
ext = buf(UBound(buf))
strSavePath = ThisWorkbook.Path & "\" & "DownloadedFile." & ext
ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
If ret = 0 Then
MsgBox "Download has been succeed!"
Else
MsgBox "Error"
End If
End Sub
Source: VBA - Save a file from a Website
Solution 2:
Option Explicit
Sub DownloadXLFileFromURL()
Dim myURL As String, sFilename As String
myURL = "http://img.chandoo.org/hw/max-change-problem.xlsx"
sFilename = Environ("SystemDrive") & Environ("HomePath") & _
Application.PathSeparator & "Desktop" & Application.PathSeparator & _
"file.xlsx"
Dim WinHttpReq As Object, oStream As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False ', "username", "password"
WinHttpReq.Send
myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile sFilename, 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End Sub
Source: Download file from URL using VBA
Solution 3:
Finally, you can create custom class, which can create an instance of MS IE. Now, you are able to control/manage of IE object via using its properties and events, such as DownloadBegin, DownloadComplete, FileDownload
Please, see: Using the WebBrowser Control from Visual Basic
Note: Never before i tried it...

Download Files (PDF) from Web

I am fresh-starter at VBA.
How do I download PDF file using UrlDownloadToFile from http://cetatenie.just.ro/wp-content/uploads/?
Can anybody help with this? The code is searching the PDF files udner hyperlinks and matches them under some criteria, i.e. the current year under their name.
Function UrlDownloadToFile(lNum As Long, sUrl As String, sPath As String, _
lNum1 As Long, lNum2 As Long) As Long
UrlDownloadToFile = 0
End Function
Sub DownPDF()
' This macro downloads the pdf file from webpage
' Need to download MSXML2 and MSHTML parsers and install
Dim sUrl As String
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Dim hAnchor As MSHTML.HTMLAnchorElement
Dim Ret As Long
Dim sPath As String
Dim i As Long
sPath = "C:\Documents and Settings\ee28118\Desktop\"
sUrl = "http://cetatenie.just.ro/wp-content/uploads/"
'Get the directory listing
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "GET", sUrl
xHttp.send
'Wait for the page to load
Do Until xHttp.readyState = 4
DoEvents
Loop
'Put the page in an HTML document
Set hDoc = New MSHTML.HTMLDocument
hDoc.body.innerHTML = xHttp.responseText
'Loop through the hyperlinks on the directory listing
For i = 0 To hDoc.getElementsByTagName("a").Length - 1
Set hAnchor = hDoc.getElementsByTagName("a").Item(i)
'test the pathname to see if it matches your pattern
If hAnchor.pathname Like "Ordin-*.2013.pdf" Then
Ret = UrlDownloadToFile(0, sUrl & hAnchor.pathname, sPath, 0, 0)
If Ret = 0 Then
Debug.Print sUrl & hAnchor.pathname & " downloaded to " & sPath
Else
Debug.Print sUrl & hAnchor.pathname & " not downloaded"
End If
End If
Next i
End Sub
Sorry - I should have guessed that URLDownloadToFile was an API call and could have answered the whole question at SQL "%" equivalent in VBA.
Remove the function named URLDownloadToFile completely. Paste this at the top of the module where your Sample procedure is
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
Now change that one line in Sample to look like this
Ret = URLDownloadToFile(0, sUrl & hAnchor.pathname, sPath & hAnchor.pathname, 0, 0)
Then you should be good to go. If you want some different file name, then you'll have to code some logic to change it at each iteration.

Difficulites in accessing the Download files dialog box - VBA Code

I have mini project in VBA (Excel). I need to open the IE and download the files (save the doc) in the designated folder in my desktop.
However, I having difficulties in accessing the Download files dialog box. Even tried Savekeys concept but in vain.
How to proceed further and save the files?
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
Sub DownloadFileFromWeb()
Dim myFile As String
Dim strSavePath As String
Dim URL As String, ext As String
URL = "www.ahma.org/Education/BRD_Hardlines_Industry_Item.doc" 'for TEST
strSavePath = "C:\Users\Yogendra.Ur\Desktop\" & "DownloadedFile" & ext
Dim buf, Ret As Long
buf = Split(URL, ".")
ext = buf(UBound(buf))
Dim IE As Object
Set IE = CreateObject("internetexplorer.application")
IE.VISIBLE = True
IE.Navigate URL
Ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
If Ret = 0 Then
MsgBox "Download has been succeed!"
Else
MsgBox "Error"
End If
End Sub
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
Sub DownloadFileFromWeb()
Dim myFile As String
Dim buf, Ret As Long
Dim strSavePath As String
Dim URL As String, ext As String
URL = "http://www.ahma.org/Education/BRD_Hardlines_Industry_Item.doc"
buf = Split(URL, ".")
ext = buf(UBound(buf))
strSavePath = "C:\local files\DownloadedFile." & ext
Debug.Print strSavePath
Ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
If Ret = 0 Then
MsgBox "Download OK!"
Else
MsgBox "Error"
End If
End Sub