VBA - 打开网站并将其另存为 .GIF 扩展名

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

我正在尝试打开一个包含图像的网页,然后将其作为 .GIF 扩展名保存到我的桌面。下面的代码为我打开了一个测试页面:

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

下一步是将页面保存为 .GIF。执行此操作的手动过程是右键单击图像并按保存,然后将 .gif 扩展名添加到名称中,或者另一种方法是在页面上按 CTRL+S 并将其另存为图像。

我尝试了 API 函数 URLDownloadToFile 但是每次刷新页面时,我用于我的应用程序的图像都会更新,我需要保存的图像作为与打开的一样,因此无法使用上述功能,因为它会导致两个不同的图像。

如果可能的话,我尽量避免为此使用 SendKeys。

根据我的评论,尝试以下操作(原始代码 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

编辑: IE 将图像存储在临时文件夹中,因此您可以从那里获取它并使用上述功能更改扩展名。

这与此处帖子的回复相同:

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