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

Excel VBA-打开网站并将其保存为.GIF扩展名,excel,vba,web,gif,save-image,Excel,Vba,Web,Gif,Save Image,我正在尝试打开一个包含图像的网页,并将其作为.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-

我正在尝试打开一个包含图像的网页,并将其作为.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
子测试()
尺寸IE作为对象,文档作为对象
设置IE=CreateObject(“internetexplorer.application”)
可见=真实
即“导航”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”)
设置文档=即文档
端接头
下一步是将页面保存为.GIF格式。手动执行此操作的过程可以是右键单击图像并按save,然后将.gif扩展名添加到名称中,也可以是在页面上按CTRL+S,然后以这种方式将其另存为图像

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


如果可能,我正在尝试避免为此使用SendKeys。

根据我的评论,请尝试以下(原始代码):

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

这与poste上的响应相同:


简言之,我的问题是,在excel VBA中,IE是否有类似Doc.SaveAs“C:\Users\aaa\Desktop\testi.gif”的东西?我认为这可以通过文件系统对象类来实现。您可能需要检查BitBlt win32 API。使用它,你可以抓取屏幕的任何部分并将其保存为图像。谢谢你的代码。它在另一个项目中帮助了我很多,但对于这个问题,它似乎没有按要求工作。我上面给出的示例图像只是一个参考测试用例,每次重新加载页面时,我处理的实际图像都会发生变化。因此,此方法保存的图像与打开的网页上的图像不同,而我尝试使保存的图像与打开的网页上的图像相同。我认为唯一可以做到这一点的方法是直接保存打开的网页,但我找不到相应的代码。是否可以修改您的代码,从浏览器上打开的网页获取图像并将其保存为.gif格式?我想这会解决问题的。对不起。照片(完整的.jpg路径)是用户与页面交互的结果吗?你可以用IE打开页面,然后下载图片。让我们来看看。
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
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