Open webpage and save image

2019-09-19 17:38发布

问题:

I am currently using the following VBA code to open the website on IE:

Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.Navigate "http://test.com"

I now need to download an image from the website as a .gif which has the following source (from using inspect element):

<img src="test.php" align="left">

Any advise on how I could do this please?

回答1:

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://upload.wikimedia.org/wikipedia/commons/0/0e/Balle.gif"
    sout = Environ("HOMEPATH") & "\Desktop\" & "image.gif"

    ret = URLDownloadToFile(0, sin, sout, 0, 0)
    If (ret = 0) Then MsgBox "Succedded" Else MsgBox "failed"
End Sub


标签: excel vba web