JasperD
JasperD

Reputation: 172

Get online image width and height vba excel

is there a way that I can get the image dimensions of an online image if I have the link? For example, how would I get width and height in VBA for https://www.google.com/images/srpr/logo11w.png

Thanks!

Jasper

Upvotes: 0

Views: 1788

Answers (1)

Automate This
Automate This

Reputation: 31364

Assuming your image URL example is pretty close to what you really want, you can do it with a little 'web scraping':

First, you must add the following two references

  1. Microsoft Internet Controls
  2. Microsoft HTML Object Library

Sub extract()
    Dim IE As InternetExplorer
    Dim html As HTMLDocument

    Set IE = New InternetExplorerMedium
    IE.Visible = False
    IE.Navigate2 "https://www.google.com/images/srpr/logo11w.png"

    ' Wait while IE loading
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    Set html = IE.document

    Dim webImage As Variant
    Set webImage = html.getElementsByTagName("img")

    'Debug.Print webImage(0).Width
    'Debug.Print webImage(0).Height

    MsgBox ("Image is " & webImage(0).Width & " x " & webImage(0).Height)

    'Cleanup
    IE.Quit
    Set IE = Nothing
End Sub

Results:

enter image description here

Upvotes: 1

Related Questions