Search code examples
excelvbawebgifsave-image

VBA - Opening a website and saving it as a .GIF extension


I am trying to open and then save a web page which contains an image as a .GIF extension to my desktop. The below code opens a test page for me:

Sub test()
    Dim IE As Object, Doc As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.Navigate "http://www.orseu-concours.com/54-189-thickbox/epso-numerical-reasoning-test-2-en.jpg"

    Do While IE.ReadyState <> 4: DoEvents: Loop
    Set Doc = CreateObject("htmlfile")
    Set Doc = IE.Document
End Sub

The next step is saving the page as a .GIF. The manual process for doing this is either right clicking the image and pressing save and then adding the .gif extension to the name or another way is to just press CTRL+S on the page and save it as an image that way.

I have tried API function URLDownloadToFile however the image I am using for my application updates every time the page is refreshed and I require the saved image to be the same as the one open therefore, cannot use the above function as it results in the two different images.

If possible, I am trying to avoid using SendKeys for this.


Solution

  • As per my comment, try the following (original code here):

    Sub main()
    'downloads google logo
    HTTPDownload "https://www.google.tn/images/srpr/logo11w.png", "d:\logo11w.png"
    End Sub
    Sub HTTPDownload(myURL, myPath)
    ' This Sub downloads the FILE specified in myURL to the path specified in myPath.
    '
    ' myURL must always end with a file name
    ' myPath may be a directory or a file name; in either case the directory must exist
    '
    ' Written by Rob van der Woude
    ' http://www.robvanderwoude.com
    '
    ' Based on a script found on the Thai Visa forum
    ' http://www.thaivisa.com/forum/index.php?showtopic=21832
    
        ' Standard housekeeping
        Dim i, objFile, objFSO, objHTTP, strFile, strMsg
        Const ForReading = 1, ForWriting = 2, ForAppending = 8
    
        ' Create a File System Object
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        ' Check if the specified target file or folder exists,
        ' and build the fully qualified path of the target file
        If objFSO.FolderExists(myPath) Then
            strFile = objFSO.BuildPath(myPath, Mid(myURL, InStrRev(myURL, "/") + 1))
        ElseIf objFSO.FolderExists(Left(myPath, InStrRev(myPath, "\") - 1)) Then
            strFile = myPath
        Else
            WScript.Echo "ERROR: Target folder not found."
            Exit Sub
        End If
    
        ' Create or open the target file
        Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)
    
        ' Create an HTTP object
        Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    
        ' Download the specified URL
        objHTTP.Open "GET", myURL, False
        objHTTP.Send
    
        ' Write the downloaded byte stream to the target file
        For i = 1 To LenB(objHTTP.ResponseBody)
            objFile.Write Chr(AscB(MidB(objHTTP.ResponseBody, i, 1)))
        Next
    
        ' Close the target file
        objFile.Close
    End Sub
    

    Edit: IE stores the image in the temp folder so you can pick it up from there and change the extension using the function above.