title: Downloading files; Executing a JavaScript function using .execScript VBA
body: <p><strong>Situation:</strong> </p>
  
  <p>I am downloading files from the webpage <a href="https://www.england.nhs.uk/statistics/statistical-work-areas/delayed-transfers-of-care/delayed-transfers-of-care-data-2017-18/" rel="nofollow noreferrer">NHS Delayed Transfers of Care</a>. </p>
  
  <p>In the HTML I can see the following:</p>
  
  <pre><code>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');"
  </code></pre>
  
  <p>After looking <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference" rel="nofollow noreferrer">here</a> and seeing these SO questions (amongst others): </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3247391/click-button-or-execute-javascript-function-with-vba">Click button or execute JavaScript function with VBA</a></li>
  <li><a href="https://stackoverflow.com/questions/31521205/how-to-find-and-call-javascript-method-from-vba">How to find and call javascript method from vba</a></li>
  <li><a href="http://www.vbaexpress.com/forum/showthread.php?9690-Solved-call-a-javascript-function" rel="nofollow noreferrer">Call a javascript function</a></li>
  </ul>
  
  <p>I am under the impression that <code>ga()</code> is a JavaScript function I should be able to call directly with <code>.execScript</code>.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Can I execute the JavaScript function using <code>.execScript</code> to download the file? If not, how can I download the file(s)?</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p>I have tried the following unsuccessfully:</p>
  
  <p>1) <code>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")</code></p>
  
  <blockquote>
    <p>'-2147352319   Automation error</p>
  </blockquote>
  
  <hr>
  
  <p>2) <code>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")</code> </p>
  
  <blockquote>
    <p>Error 438 Object doesn't support this property or method</p>
  </blockquote>
  
  <hr>
  
  <p>3) <code>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")</code> </p>
  
  <blockquote>
    <p>Error 91 Object variable or With block variable not set</p>
  </blockquote>
  
  <hr>
  
  <p>4) <code>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")</code> </p>
  
  <blockquote>
    <p>-2147352319  Could not complete the operation due to error 80020101.</p>
  </blockquote>
  
  <p>I will admit to very little knowledge of these sorts of operations. Can anyone see where I am going wrong please?</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><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 &lt;&gt; 0 Then Debug.Print Err.Number, Err.Description
  End Sub
  </code></pre>
  
  <p><strong>References added:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Ax0k6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ax0k6.png" alt="References in project"></a></p>
  
  <p>Here is a reduced version of the HTML. Apologies, I am not used to formatting HTML.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p&gt;
    &lt;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');"&gt;Total Delayed Days Local Authority 2017-18 November (XLS, 121KB)&lt;/a&gt;
    &lt;br&gt;
  &lt;/p&gt;</code></pre>
  </div>
  </div>
  </p>