title: Save Image from URL. Fails inside a loop, but not when run once
body: <p>This Function works:</p>
  
  <pre><code>Sub SaveImage(url, name)
      ' Set your settings
      strFileURL = url
      strHDLocation = "D:\Images\" &amp; name
  
      ' Fetch the file
      Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
  
      objXMLHTTP.open "GET", strFileURL, false
      objXMLHTTP.send()
  
      If objXMLHTTP.Status = 200 Then
      Set objADOStream = CreateObject("ADODB.Stream")
      objADOStream.Open
      objADOStream.Type = 1 'adTypeBinary
  
      objADOStream.Write objXMLHTTP.ResponseBody
      objADOStream.Position = 0 'Set the stream position to the start
  
      Set objFSO = Createobject("Scripting.FileSystemObject")
      If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
      Set objFSO = Nothing
  
      objADOStream.SaveToFile strHDLocation
      objADOStream.Close
      Set objADOStream = Nothing
      End if
  
      Set objXMLHTTP = Nothing
  End Sub
  </code></pre>
  
  <p>If I call it once, it download the image, if I put it inside a loop it fail with different errors.
  I set this function in a page, and set the URL and image name, then it download the image.
  I have permission to everyone. NO PERMISSION ISSUE.
  If I put it in a seperate page, then take the URL and image name from request, as:</p>
  
  <pre><code>Dim url, name
  url = Request("url")
  name = Request("name")
  </code></pre>
  
  <p>Then call the function:</p>
  
  <pre><code>Call SaveImage(url, name)
  </code></pre>
  
  <p>I get:</p>
  
  <pre>
  ADODB.Stream error '800a0bbc'
  Write to file failed.
  </pre>
  
  <p>If I call it from the page, like this:</p>
  
  <pre><code>Call SaveImage("http://www.example.com/images/imageName.jpg", "imageName.jpg")
  </code></pre>
  
  <p>It dose download the image.</p>
  
  <p>But if from the same place, I call the sub from inside a loop, like this:
  Variables are declared above. Array is filled.</p>
  
  <pre><code>For row = 0 To UBound(myArray, 2)
      url = CStr(ar(1, row)) : image = id &amp; ".jpg"
      Call SaveImage2(url, image)
  Next
  </code></pre>
  
  <p>It fail in the first image.</p>
  
  <pre>
  ADODB.Stream error '800a0bbc'
  Write to file failed.
  </pre>
  
  <p>I tried others stream writers, but others act the same with different errors.</p>