Download google finance file from internet using VBA - excel

I am doing a VBA module in which I want to download the csv file from google finance.
Here is a sample URL
www.google.com/finance/historical?q=LON%3AESSR&ei=dEfGU_ioD4iKwAO3-4HQDg&output=csv
Once I put this URL in browser I get a csv file downloaded. But When I use the "WinHttpReq" object to download the file, I am not getting the csv file. Instead I am getting some HTML.
Is there anyway for me to download this CSV file using the URL in VBA?
Thanks,
Arun.

I am not familiar with WINHTTP but I seem to get this working fine - with XMLHTTP. Just enter your file path & name where commented
Sub getCSV()
Dim xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
xhr.Open "GET", "http://www.google.com/finance/historical?q=LON%3AESSR&ei=dEfGU_ioD4iKwAO3-4HQDg&output=csv", False
xhr.send
With xhr
If .Status = 200 And .ReadyState = 4 Then
myfile = FreeFile
'HERE
Open "FILE_PATH_&_NAME.csv" For Output As #myfile
Print #myfile, .responseText
Close #myfile
Set xhr = Nothing
End If
End With
End Sub

Related

Multiple files download using Internet Explorer, rename them and save to the folder

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

Excel VBA - Downloaded csv file with url but it just displays html code instead of actual contents of csv file

There is an online spreadsheet that has the option to "Download Spreadsheet". When you select it, it downloads the file as a .csv to the downloads folder. I want to download this spreadsheet as part of a macro and save it to a specific folder. This is meant for multiple users, so I don't want to assume the download will go to the downloads folder for every user. I've previously used code similar to the code below (found online) with success. For this link, though, it downloads a .csv file that appears to have about 1500 lines of html code instead of the actual spreadsheet. Do you know if there is either some way to tweak the actual code to get the spreadsheet itself or any other suggestions for possibly getting a different url? There is some sensitive information, so I'm just sharing part of the "html" .csv and the relevant code I've used, but please let me know if there's any other info you need.
Sub ...()
Dim ...
DT_Url = "https://.......com/db/bpmcgi93a?a=q&qid=501&dlta=xs%7E&ridlist=697"
DTFile = "Deal_Tracker_Que_Report" & Range("Today") & ".csv"
DT_Save = ThisWorkbook.Path & "\MF Historical Queues\" & DTFile
DTFilePath = Range("Newest_Queue_Path") & DTFile
Download_Queue DT_Url, DT_Save
Set DT_Q = Workbooks.Open(DTFilePath)
ActiveSheet.Name = "DT_Que"
End Sub
Sub Download_Queue(myURL, saveFile)
Dim HttpReq As Object
Set HttpReq = CreateObject("Microsoft.XMLHTTP")
HttpReq.Open "GET", myURL, False, "username", "password"
HttpReq.Send
myURL = HttpReq.responseBody
If HttpReq.Status = 200 Then
Set oStrm = CreateObject("ADODB.Stream")
oStrm.Open
oStrm.Type = 1
oStrm.Write HttpReq.responseBody
oStrm.SaveToFile saveFile, 2 ' 1 = no overwrite, 2 = overwrite
oStrm.Close
End If
End Sub
Link to download spreadsheet
.csv file with html code

Saving a file from a website path linking the file using VBA

I'm trying to export an excel file from a website. The path that I am provided already clicks the export the file and opens a prompt that asks what I want to do with the file. How do I access the prompt box to open the file?
I've tried the code where you open the path to the google gif but it doesn't work with this since the path doesn't really lead to my content. My goal is to open the excel file and export the content to my main excel file (the one containing the macro).
Ive tried this but this only works on direct link to content and not to saving prompt.
Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean
Dim oXMLHTTP As Object, i As Long, vFF As Long, oResp() As Byte
'You can also set a ref. to Microsoft XML, and Dim oXMLHTTP as MSXML2.XMLHTTP
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open "GET", vWebFile, False 'Open socket to get the website
oXMLHTTP.send 'send request
'Wait for request to finish
Do While oXMLHTTP.readyState <> 4
DoEvents
Loop
oResp = oXMLHTTP.responseBody 'Returns the results as a byte array
'Create local file and save results to it
vFF = FreeFile
If Dir(vLocalFile) <> "" Then Kill vLocalFile
Open vLocalFile For Binary As #vFF
Put #vFF, , oResp
Close #vFF
'Clear memory
Set oXMLHTTP = Nothing
End Function
This works if the path is the web content itself. I need work from a path that initiates download of the file. Thanks!

VBA WinHTTP to download file from password proteced https website - saved file corrupted

I have searched for a solution to this issue having first researched via Mr Excel and also elsewhere on this site (specifically question 22051960 which appears to be closed to new users like me).
The site I am attempting to download from is :
https://downloads.theice.com/
and it appears that the main site is an html page requesting authorisation credentials.
I have tried the code the above referenced thread which appears to successfully open the main site, authenticate and save the file; however, when I navigate to the file it is not the file on the site but is instead only 1kb and not in excel format. Here is the code from that thread:
Sub SaveFileFromURL()
Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object
mainUrl = "https://downloads.theice.com/"
fileUrl = "https://downloads.theice.com/Settlement_Reports/Oil/icecleared_oil_2017_01_24.xlsx"
filePath = "C:\mydownloads\myfile2.xls"
myuser = "username"
mypass = "password"
'#David Zemens, I got this by examining webpage code using Chrome, thanks!
strAuthenticate = "start-url=%2F&user=" & myuser & "&password=" & mypass & "&switch=Log+In"
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
'I figured out that you have to POST authentication string to the main website address not to the direct file address
WHTTP.Open "POST", mainUrl, False 'WHTTP.Open "POST", fileUrl, False
WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.Send strAuthenticate
'Then you have to GET direct file url
WHTTP.Open "GET", fileUrl, False
WHTTP.Send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
'Save the file
FileNum = FreeFile
Open filePath For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
MsgBox "File has been saved!", vbInformation, "Success"
End Sub
I have reasonable vba skills within excel but have no experience of html or web-page functionality and so am lost as to how to resolve this issue.
My ultimate aim is to utilise the authentication code within a routine I have written which automatically saves files from a list of URL's in an excel spreadsheet which already works for non-protected url's.
I hope this is an acceptable question for this forum
Many thanks
In my opinion, you should set the credentials (SetCredentials method) before sending your request on your WinHttpRequest instance.
Something like this
WHTTP.SetCredentials(myuser , mypass , HTTPREQUEST_SETCREDENTIALS_FOR_SERVER);
Could you also have a look on below links?
https://msdn.microsoft.com/en-us/library/aa384058(VS.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa383144(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384076(v=vs.85).aspx
I hope this will help you

Not able to open downloaded Excel file from Sharepoint 2013 using Excel VBA

i have tried the following code to download a single Excel file from Sharepoint 2013.
Option Explicit
Sub TxtStream()
Dim myURL As String, DestFile As String, myHeader As String
Dim Usr as string, Pwd as string
Dim oStream As Object
Usr="": Pwd=""
myURL = "https://Server.Name/teams/Forms/AllItems.aspx/MasterFile.xlsx"
DestFile = "C:\Test.xlsx"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("MSXML2.XMLHTTP")
'WinHttpReq.Open "HEAD", myURL, False, Usr, Pwd
'WinHttpReq.Send
'myHeader = WinHttpReq.getAllResponseHeaders()
'Debug.Print myHeader
'myHeader = WinHttpReq.getResponseHeader("Content-Disposition")
'Debug.Print myHeader
'myHeader = WinHttpReq.getResponseHeader("Content-Type")
'Debug.Print myHeader
WinHttpReq.Open "GET", myURL, False, Usr, Pwd
WinHttpReq.setRequestHeader "content-type", "application/octet-stream"
WinHttpReq.Send
myURL = WinHttpReq.responsebody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Type = 1
oStream.Open
oStream.Position = 0
oStream.Write WinHttpReq.responsebody
oStream.SaveToFile DestFile, 2
oStream.Close
End If
End Sub
It downloads ok, but when i try to open it in Excel 2010, it shoots an error:
Excel cannot open the file Test.xlsx because the file format or file extension is not valid. Verify the file has not been corrupted and the file extension matches the format of the file.
I have checked the Content-Type and it shows as Text/Html;UTF-8. It doesn't show Content-Disposition.
Can someone help as to why the file is not opening?
This is not a full fledged answer, but more like a comment / suggestion as I do not have enough reputation to post comments.
Setting a content-type header in your GET query does not seems to make any difference. We need to check the WinHttpReq.getResponseHeader("Content-Type") sent by the server which in this case seems to be Text/Html;UTF-8.
If you copy paste the URL used in myURL variable to your browser, do you get the "file save as" dialog box?
Better analysis of your requests and responses can be done if you use Fiddler Web Debugger where you can create requests in teh composer and check the responses to finetune your requirements and to see whether any other headers (like cookies, authentications etc) are also required for the file to be downloaded. In this case, you might be getting back a login page (html) because of some authentication issues and not the excel file.
Open the DestFile saved with Notepad.exe to see whether it is actualy an XLSX file (starting with characters PK) or an HTML or other text file.

Resources