在VBA中获取属性值

3

我需要制作一个VBA文件,它可以读取一个网页并返回IMG标签的SRC属性的值。我无法使最后一步工作。你们能帮我吗?

<html>
<body>
<img src="image.jpg">
</body>
</html>

===编辑=== 我成功返回了属性对象。现在需要返回它的值。

Option Compare Database

Sub AcessaPagina()
    Dim ie As InternetExplorer
    Dim test As String
    Dim obj As Object

    Set ie = New InternetExplorer
    ie.Navigate "http://www.google.com.br"
    MsgBox ie.Document.getElementsByTagName("img").Item(0).Attributes("src")
    ie.Visible = True 
End Sub

这是我目前拥有的内容。
1个回答

6

"getElementByTagName"这个方法是不存在的 -- 正确的方法名为getElementsByTagName (注意要加上s,因为它返回的是一个集合)

文档对象会返回源代码中所有的img标签集合。因此,可以像如下一样进行迭代:

Sub AcessaPagina()
    Dim ie As Object ' InternetExplorer
    Dim images As Object ' MSHTML.IHTMLElementCollection
    Dim image As Object ' MSHTML.IHTMLElement

    Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate "http://www.google.com.br"
    Set images = GetAllImages(ie)

    For Each image In images
      Debug.Print image.getAttribute("src")
    Next image

End Sub

Function GetAllImages(ie As Object) As Object
  Set GetAllImages = ie.document.images
End Function

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接