0

I've been trying to download a dancing banana Png lately (just to learn how) and have just not been having any luck. Whenever I try something out it gives me an error that says Write to File failed and gives me 800A0BBC as the code. What am I doing wrong? Thanks in advance! Code:

dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "https://wallpapercave.com/wp/wp5042624.png", False
xHttp.Send

with bStrm
  .type = 1 '//binary
  .open
  .write xHttp.responseBody
  .savetofile "c:\temp\wp5042624.png", 2 '//overwrite
end with
user692942
  • 16,398
  • 7
  • 76
  • 175
Bob
  • 19
  • 3

1 Answers1

0

I don't know if you have in your code that you tried before an extra quote or your code is not well formatted; anyway , give a try for this code that save your image in a folder named Images_PNG created on your desktop just for testing !


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Ws = CreateObject("WScript.Shell")
strDirectory = "Images_PNG"
strDirectory = objFSO.BuildPath(Ws.SpecialFolders("Desktop"), strDirectory)
If not objFSO.FolderExists(strDirectory) Then objFSO.CreateFolder(strDirectory)
URL = "https://wallpapercave.com/wp/wp5042624.png"
Save2File = strDirectory & "\wp5042624.png"
Call Download(URL,Save2File)
MsgBox "Terminted !",vbInformation,"Download PNG File"
'--------------------------------------------------------------------------------------------
Sub Download(URL,Save2File)
Dim File,Line,BS,ws
    On Error Resume Next
    Set File = CreateObject("Microsoft.XMLHTTP")
    File.Open "GET",URL, False
    File.Send()
    If err.number <> 0 then
        Line  = Line &  vbcrlf & "Error Getting File"
        Line  = Line &  vbcrlf & "Error " & err.number & "(0x" & hex(err.number) & ") " &  vbcrlf &_
        err.description
        Line  = Line &  vbcrlf & "Source " & err.source
        MsgBox Line,vbCritical,"Error getting file"
        Err.clear
        wscript.quit
    End If
    If File.Status = 200 Then ' File exists and it is ready to be downloaded
        Set BS = CreateObject("ADODB.Stream")
        Set ws = CreateObject("wscript.Shell")
        BS.type = 1
        BS.open
        BS.Write File.ResponseBody
        BS.SaveToFile Save2File, 2
    ElseIf File.Status = 404 Then
        MsgBox  "File Not found : " & File.Status,vbCritical,"Error File Not Found"
    Else
        MsgBox "Unknown Error : " & File.Status,vbCritical,"Error getting file"
    End If
End Sub
'-------------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70