Situation:
I am downloading files from the webpage NHS Delayed Transfers of Care.
In the HTML I can see the following:
onclick="ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');"
After looking here and seeing these SO questions (amongst others):
- Click button or execute JavaScript function with VBA
- How to find and call javascript method from vba
- Call a javascript function
I am under the impression that ga() is a JavaScript function I should be able to call directly with .execScript.
Question:
Can I execute the JavaScript function using .execScript to download the file? If not, how can I download the file(s)?
What I have tried:
I have tried the following unsuccessfully:
1) Call html.parentWindow.execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript")
'-2147352319 Automation error
2) Call html.frames(0).execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript")
Error 438 Object doesn't support this property or method
3) Call currentWindow.execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript")
Error 91 Object variable or With block variable not set
4) Call CurrentWindow.execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript")
-2147352319 Could not complete the operation due to error 80020101.
I will admit to very little knowledge of these sorts of operations. Can anyone see where I am going wrong please?
Code:
Option Explicit
Public Sub DownloadDTOC()
Dim http As New XMLHTTP60
Dim html As New HTMLDocument
Dim CurrentWindow As HTMLWindowProxy
With http
.Open "GET", "https://www.england.nhs.uk/statistics/statistical-work-areas/delayed-transfers-of-care/delayed-transfers-of-care-data-2017-18/", False
.send
html.body.innerHTML = .responseText
End With
On Error GoTo Errhand
'Call html.parentWindow.execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript") '-2147352319 Automation error
'Call html.frames(0).execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript") '438 Object doesn't support this property or method
'automation error
'Call currentWindow.execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript") ' 91 Object variable or With block variable not set
Set CurrentWindow = html.parentWindow
Call CurrentWindow.execScript("ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');", "Javascript") '--2147352319 Could not complete the operation due to error 80020101.
Exit Sub
Errhand:
If Err.Number <> 0 Then Debug.Print Err.Number, Err.Description
End Sub
References added:
Here is a reduced version of the HTML. Apologies, I am not used to formatting HTML.
<p>
<a href="https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls" class="xls-link" onclick="ga('send', 'event', 'Downloads', 'XLS', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/01/LA-Type-B-November-2017-2ayZP.xls');">Total Delayed Days Local Authority 2017-18 November (XLS, 121KB)</a>
<br>
</p>


.execScriptwell. Have you tried opening it through a hidden IE window and then executing your script?xls-link?onclickis also available within that class. However, what I wanna say isxmlhttp60request won't be able to fetch you anything from that page because it can't even parse the text within that class. The content of that website is generated dynamically. You should go for IE.