如何使getElementsBy *结果变暗 [英] How to Dim a getElementsBy* result
问题描述
我正在从VBA进行一些Internet搜索,有一点我就知道了
I am doing some internet searching from VBA, and at one point I have the line
Set valueResult = currPage.getElementById("rg_s").getElementsByTagName("IMG")
其中 currPage
被声明为 HTMLDocument
而且我想知道如何 Dim
valueResult
来实现:
and I'm wondering how to Dim
valueResult
to achieve:
- IntelliSense(vba的自动完成功能)结果
- 更好的执行时间(通过使用特定而不是默认的
Variant
类型)
当我查看本地"窗口时,被告知 Set
为它提供了 DispHTMLElementCollection
类型,但是当我 Dim
时,这不是选项.我已经阅读了getElementsBy *
实际上返回 节点列表
而不是数组,因此我一直在尝试采用这种方法,但是找不到特定于VBA的任何东西.
When I look at the Locals window, I'm told that Set
gives it the
DispHTMLElementCollection
type, but that's not an option when I Dim
. I've read that getElementsBy*
actually returns a Node List
as opposed to an array, so I've been trying to follow that avenue, but can't find anything VBA specific.
那我应该怎么声明呢?-目前,我刚刚获得了 Dim valueResult as Object
,但这并不比 Variant
好并且不提供IntelliSense提示.
So how should I declare it? - at the moment I've just got Dim valueResult As Object
but that's hardly better than Variant
and doesn't give IntelliSense prompts.
NB.我勾选了Microsoft HTML对象库
推荐答案
它应该是 IHTMLElementCollection
类型.
Dim valueResult As IHTMLElementCollection
'// your code here
Set valueResult = currPage.getElementById("rg_s").getElementsByTagName("IMG")
getElementsByTagName()
方法将返回 Collection
,因为可能存在多个匹配项. getElementById()
方法将返回一个元素,因此 IHTMLElement
The getElementsByTagName()
method will return a Collection
because there are potentially multiple matches. The getElementById()
method will return an element, so IHTMLElement
快速示例:
Sub test()
Dim col As IHTMLElementCollection
Dim item As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://www.google.co.uk"
While IE.ReadyState <> 4
DoEvents
Wend
Set col = IE.Document.getElementsByTagName("a")
For Each i In col
Set item = i
Debug.Print item.outerText
Next
IE.Quit
End Sub
这篇关于如何使getElementsBy *结果变暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!