excel macro to capture the output image file from website

kiwiques

New Member
Joined
Sep 25, 2014
Messages
7
Hi,
I have an excel with a column listed with URLs. These URLs would direct me to image files. What I need to do is only verifying if such image file exists. The problem I have now is: Whenever the image does not exist, the website would replace it with "imagenotfound.png", making me really difficult to check for that image existence.

In that case, would it be better to check its output filename instead? How?

Pls kindly help. Thanks.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Not sure if anyone can kindly help? I am desperate to get this task completed by today. Many thanks in advanced.
 
Upvote 0
Try this, however without example URLs I'm unable to test it with your data. Put this code in a VBA module and call it as a cell formula like this:

=Image_Exists(A2)

where A2 contains a URL. It returns TRUE as the cell value if the image exists, or FALSE if the image src contains "imagenotfound.png".

Code:
Public Function Image_Exists(URL As String) As Boolean

    Static httpReq As Object
    Dim HTMLdoc As Object
    Dim images As Object
    
    Image_Exists = False
    
    If httpReq Is Nothing Then Set httpReq = CreateObject("MSXML2.XMLHTTP")
    
    With httpReq
        .Open "GET", URL, False
        .send
        Set HTMLdoc = CreateObject("HTMLfile")
        HTMLdoc.body.innerHTML = .responseText
    End With
    
    Set images = HTMLdoc.getElementsByTagName("IMG")
    If images.Length > 0 Then
        Image_Exists = InStr(1, images(0).src, "imagenotfound.png", vbTextCompare) = 0
    End If
    
End Function
 
Last edited:
Upvote 0
Thanks John - it seems the output is always FALSE even though the image is found. Can you help further? thx.
 
Upvote 0
As I hinted, post some example URLs: some which direct to a valid image and some which don't.
 
Upvote 0
Try this UDF:
Code:
Public Function Image_Exists(URL As String) As Boolean

    Static httpReq As Object
    
    If httpReq Is Nothing Then Set httpReq = CreateObject("MSXML2.XMLHTTP")
    With httpReq
        .Open "GET", URL, False
        .send
        Image_Exists = InStr(.getResponseHeader("Content-Disposition"), "imagenotfound.png") = 0
    End With
        
End Function
 
Upvote 0

Forum statistics

Threads
1,217,414
Messages
6,136,488
Members
450,016
Latest member
murarj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top