• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Save web file error

YasserKhalil

Well-Known Member
Hello everyone
I have found the following code
Code:
Sub Test()
Dim strPath As String

strPath = ThisWorkbook.Path & "\websitelogo.png"
    Call SaveWebFile("http://www.bettersolutions.com/websitelogo.png", strPath)
    ActiveSheet.Pictures.Insert strPath
    Kill strPath
End Sub

Public Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean
Dim oXMLHTTP As MSXML2.XMLHTTP
Dim i As Long
Dim vFF As Long
Dim oResp() As Byte

    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    oXMLHTTP.Open "GET", vWebFile, False
    oXMLHTTP.send
   
    Do While (oXMLHTTP.readyState <> 4)
        DoEvents
    Loop
   
    oResp = oXMLHTTP.responseBody
   
    vFF = FreeFile
    If Dir(vLocalFile) <> "" Then
        Kill vLocalFile
    End If
    Open vLocalFile For Binary As #vFF
    Put #vFF, , oResp
    Close #vFF
   
    Set oXMLHTTP = Nothing
End Function

But I got an error like that
Untitled.png

Any help please?
 
I have solved that by replacing that line
Code:
Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")

But I would like to know why I encountered this error?
 
Although my solution is not gonna fit for the answer to your question, It will give you lesser pain to serve the same purpose.

Code:
Sub SavingFiles()
    Dim http As New ServerXMLHTTP60
    Dim adob As New ADODB.stream, URL As String
    Dim htmla As Object, stream As Object, tempArr As Variant
   
    URL = "http://www.bettersolutions.com/websitelogo.png"
    tempArr = Split(URL, "/")
    tempArr = tempArr(UBound(tempArr))

    With http
        .Open "GET", URL, False
        .send
    End With
   
    With adob
        .Open
        .Type = 1
        .write http.responseBody
        .SaveToFile (ThisWorkbook.Path & tempArr)
        .Close
    End With
End Sub

Reference to add to the library:
"Microsoft ActiveX Data Objects 6.0 Library"
 
Thank you very much my friend for your solution ..
The problem is not in the code itself as I have recently suffered that error more than once and I thought there is a direct solution. I have searched a lot and found nothing to solve it till now ..

Best and Kind Regards
 
Thank you very much my friend for your solution ..
The problem is not in the code itself as I have recently suffered that error more than once and I thought there is a direct solution. I have searched a lot and found nothing to solve it till now ..

Best and Kind Regards
On which line do you get error shown in snapshot?
 
Back
Top