在 VBA 中获取属性值

发布于 2024-12-17 06:37:13 字数 563 浏览 3 评论 0原文

我需要制作一个 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

这就是我现在所拥有的。

I need to make a VBA file that will read a webpage and return the value of the SRC attribute of the IMG tag. I wasn't able to make the last step work. Can you guys help me?

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

===Edit===
I managed to return the attribute object. Now I need to return its value

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

That's what I have at the moment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-12-24 06:37:13

没有名为“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

There is no method called "getElementByTagName" -- it's called getElementsByTagName (note the s because it is a collection)

The Document Object returns a collection of all the img tags in the source. So you can iterate it like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文