title: URLDownloadToFile supposedly works, but file is corrupt or not right
body: <p>I have been using the URLDownloadToFile function successfully for over a year, but recently it just stopped working (or something with the network did).</p>
  
  <p>The relevant line of code:
  <code>success = URLDownloadToFile(0, URL, Target, 0, 0)</code> </p>
  
  <p>Upon trying to open it I get the following message:</p>
  
  <blockquote>
    <p>Excel cannot open the file 'wkbk.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.</p>
  </blockquote>
  
  <p>When I open up the file in my text editor, I see that it's really HTML:
  <strong><em>I have star'd out the domain and the path for <code>window.location...</code> for security reasons</em></strong>
      </p>
  
  <pre><code>&lt;script src="/edms/scripts/JRETest.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  
  function setCookie(cname, cvalue, exdays) {
      var d = new Date();
      d.setTime(d.getTime() + (exdays*24*60*60*1000));
      var expires = "expires="+d.toGMTString();
      document.cookie = cname + "=" + cvalue + "; " + expires + ";domain=***;path=/";
  }    
  
  if (JRETest.versionCheck('1.8+')) {
      setCookie("JRECookie", "1.7", 5);
  } else if (JRETest.versionCheck('1.7+')) {
      setCookie("JRECookie", "1.7", 5);
  } else if (JRETest.versionCheck('1.6+')) {
      setCookie("JRECookie", "1.6", 5);
  } else  {
      setCookie("JRECookie", "1.6", 5);
  }
  window.location = "&lt;link that I passed to URLDownloadToFile&gt;";
  
  &lt;/script&gt; 
  &lt;/html&gt;
  </code></pre>
  
  <p>But the thing is, when I put in the URL for the file into my browser, it automatically downloads it just like it always did, so the URL is correct, and if I run this <code>GetURLStatus</code> function, it returns <code>200 - OK</code></p>
  
  <pre><code>Function GetURLStatus(ByVal URL As String, Optional AllowRedirects As Boolean)
      Const WinHttpRequestOption_UserAgentString = 0
      Const WinHttpRequestOption_EnableRedirects = 6
  
      On Error Resume Next
      Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
      If httpRequest Is Nothing Then
          Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5")
      End If
      Err.Clear
      On Error GoTo 0
  
      httpRequest.Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
      httpRequest.Option(WinHttpRequestOption_EnableRedirects) = AllowRedirects
  
      'Clear any pervious web page source information
      PageSource = ""
  
      'Add protocol if missing
      If InStr(1, URL, "://") = 0 Then
          URL = "http://" &amp; URL
      End If
  
      'Launch the HTTP httpRequest synchronously
      On Error Resume Next
      httpRequest.Open "GET", URL, False
      If Err.Number &lt;&gt; 0 Then
        'Handle connection errors
          GetURLStatus = Err.Description
          Err.Clear
          Exit Function
      End If
      On Error GoTo 0
  
      'Send the http httpRequest for server status
      On Error Resume Next
      httpRequest.Send
      httpRequest.WaitForResponse
      If Err.Number &lt;&gt; 0 Then
        ' Handle server errors
          PageSource = "Error"
          GetURLStatus = Err.Description
          Err.Clear
      Else
        'Show HTTP response info
          GetURLStatus = httpRequest.Status &amp; " - " &amp; httpRequest.StatusText
        'Save the web page text
          PageSource = httpRequest.responsetext
      End If
      On Error GoTo 0
  End Function
  </code></pre>
  
  <p>Like I said, this all worked before, so I don't know if the internal script changed or if it's probably with our internal documentation system, and if there's any other way I can call URLDownloadToFile to either wait for a bit until the location changes, or what is really going on.</p>