1

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.

Suraj Khosla
  • 151
  • 1
  • 3
  • 11

2 Answers2

1

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.

Community
  • 1
  • 1
Amen Jlili
  • 1,884
  • 4
  • 28
  • 51
  • Thank you for this code it has helped me a lot with another project but for this problem it didn't seem to work as required. The example image I gave above was just a reference test case, the actual image I am working with changes every time the page reloads therefore, this method saves an image different to the one on the open webpage whereas I am trying to have the saved image the same as the one on the open webpage. The only way I think this can be done is by saving the open web page directly but I cannot find the code for this. – Suraj Khosla Mar 01 '15 at 17:22
  • Is there any way your code can be modified to get the image from the open webpage on the browser and save it as a .gif? I think this would solve the problem. – Suraj Khosla Mar 01 '15 at 17:27
  • I'm sorry. Is the photo (the full path of the .jpg) a result of some user interaction with the page? – Amen Jlili Mar 01 '15 at 17:28
  • You can open the page with IE and then download the image. – Amen Jlili Mar 01 '15 at 17:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71992/discussion-between-suraj-khosla-and-jlili-aman). – Suraj Khosla Mar 01 '15 at 17:32
0

this is the same resonse on the poste here: Open webpage and save image

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long





Private Sub Command1_Click()
    Dim sin As String
    Dim sout As String

    Dim ret As Long

    sin = "https://www.google.tn/images/srpr/logo11w.png"
    sout = Environ("HOMEPATH") & "\Desktop\" & "logo11w.png"

    ret = URLDownloadToFile(0, sin, sout, 0, 0)
    If (ret = 0) Then MsgBox "Succedded" Else MsgBox "failed"
End Sub
Community
  • 1
  • 1
milevyo
  • 2,165
  • 1
  • 13
  • 18