在VBA中获取属性值

时间:2011-11-21 15:03:56

标签: dom vba access-vba getelementsbytagname

我需要创建一个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 个答案:

答案 0 :(得分:6)

没有名为&#34; getElementByTagName&#34; - 它被称为 getElementsByTagName (请注意 s ,因为它是一个集合)

Document Object返回源代码中所有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