I want to make a VBA code which would download multiple files from a given URL using Internet Explorer. Code should execute following steps:
- Opens IE in the background and download files from a URL link. Basically, to download multiple files, the file code in the middle of the URL should be changed as well as the file name in the end of the URL. So, each file has it specific code (E.g for the first file the code is "45551", for second file - "45552" and etc) and names (first file - "ReportRU2021.10.04.xlsx", second file - "ReportUA2021.10.04.xlsx"). As you can see in names of files there is a date and I want to ignore it, so it seems to me wildcard character "*" can be used.
- After step 1, the code should rename these documents by removing the date in the end (E.g So for the first file it should be "ReportRU.xlsx", for File2 it should be "ReportUA.xlsx" and etc). Files which are downloaded are in .xlsx format;
- Save renamed files to the specific folder
I tried to make all the above steps as a code and my attemp is below. So can anyone help me with all of these steps? Thank you very much for your help in advance!
Sub FileFromURLDownloader()
Dim IE As Object, HTMLDoc As Object, URL$
Application.ScreenUpdating = False
Set IE = CreateObject("internetexplorer.application")
filenames = Array("ReportRU*", "ReportUA*")
filecode = Array("45551", "45552")
URLPath = "https://website/api/v1/reportdata/" & filecode & "/filename/" & filenames & "*" & ".xlsx"
For Each file In filenames
With IE
.navigate (URL)
.Visible = False
End With
Application.SendKeys "{TAB}{TAB}{ENTER}"
Application.ScreenUpdating = True
End Sub