VBA getelementsbytagname问题 [英] VBA getelementsbytagname issue

查看:45
本文介绍了VBA getelementsbytagname问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我正在尝试提取HTML表格信息并在excel电子表格中整理结果.

我正在使用 getelementsbytagname("table")(0)函数提取HTML表格信息,效果很好.桌子后,有人可以告诉我(0)的意义是什么吗?

此外,我有一个实例,其中打开的网页没有任何表信息要处理(直到打开页面时我才知道),这会导致我尝试初始化数据时代码出错表格尺寸的数组.有没有一种方法可以从 getelementsbytagname("table")(0)中提取结果,我已经尝试过:-

 如果(iDom.getelementsbytagname("table")(0)= 0)然后 

但是这会返回运行时错误.预先非常感谢您的帮助.

解决方案

首先将引用添加到 Microsoft Internet控件(SHDocVw) Microsoft HTML对象库:

然后 Object Explorer 是您的朋友:

因此, getElementsByTagName 返回具有属性 length IHTMLElementCollection .在页面上找到某些具有特定标签名称的元素时, length 大于零.HTH

 将表昏暗为IHTMLElementCollection设置表格= doc.getElementsByTagName("table")如果是表格,则长度>0然后昏暗表作为HTMLTable设置表= tables.item(0)'因为item是IHTMLElementCollection的默认属性,所以我们可以简单地设置表=表(0)'这与表相同.项目(0)万一 

Good morning,

I'm attempting to extract HTML table information and collate results on en excel spreadsheet.

I'm using the getelementsbytagname("table")(0) function to extract the HTML table info, which has worked well. Can someone please tell me what is the significance of the (0) after the table?

Also, I have an instance where an opened webpage does not have any table information to process (I don't know this until the page is opened), this leads to an error in my code as I try to initialize my data array to the table dimensions. Is there a way of extracting a result from getelementsbytagname("table")(0), I've tried:-

If (iDom.getelementsbytagname("table")(0) = 0) Then

but this returns a run time error. Many thanks in advance for your help.

解决方案

First add reference to Microsoft Internet Controls (SHDocVw) and to Microsoft HTML Object Library:

Then the Object Explorer is your friend:

So getElementsByTagName returns IHTMLElementCollection which has property length. When on the page some elements with specific tag name are found then length is greater then zero. HTH

Dim tables As IHTMLElementCollection
Set tables = doc.getElementsByTagName("table")
If tables.Length > 0 Then
    Dim table As HTMLTable
    Set table = tables.item(0)
    ' Because item is the default property of IHTMLElementCollection we can simplyfy
    Set table = tables(0) ' this is the same as tables.item(0)
End If

这篇关于VBA getelementsbytagname问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆