获取在线图像宽度和高度vba excel

时间:2015-07-02 19:11:51

标签: vba excel-vba excel

如果我有链接,有没有办法可以获得在线图像的图像尺寸? 例如,如何在VBA中为https://www.google.com/images/srpr/logo11w.png

获取宽度和高度

谢谢!

碧玉

1 个答案:

答案 0 :(得分:1)

假设您的图片网址示例非常接近您真正想要的,您可以通过一些“网页抓取”来实现:

首先,您必须添加以下两个引用

  1. Microsoft Internet Controls
  2. Microsoft HTML对象库
  3. 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
    

    结果:

    enter image description here