简体   繁体   English

Excel VBA:使用 HTML DOM

[英]Excel VBA: using HTML DOM

I need to know what all can I do with HTML over Excel VBA.我需要知道我可以通过 Excel VBA 使用 HTML 做什么。 for example I know that I can find element by id ie.document.getElementByID() .例如,我知道我可以通过 id ie.document.getElementByID()找到元素。 I will work with HTML table which doesn't have elements with id, so that it will look like child->child->sibling->child..... i think.我将使用没有带有 id 的元素的 HTML 表,因此它看起来像child->child->sibling->child.....我想。

Can anybody pleas show me part of code, which will get text "hello" from this example table?任何人都可以向我展示部分代码,从这个示例表中得到文本“你好”吗? first node will be found by his ID.第一个节点将通过他的 ID 找到。

...
<table id="something">
  <tr>
    <td></td><td></td>
  </tr>
  <tr>
    <td></td><td>hello</td>
  </tr>
...

I'm looking at this type of thing at the moment...我现在正在看这种类型的东西......

I believe the something like the below should do it:我相信像下面这样的事情应该这样做:

ie.getelementbyid("something").getelementsbytagname("TD")(3).innertext

How it works:这个怎么运作:

It searches the HTML doc for the element where the ID is equal to "something" (will take first iteration if more than 1 but you can loop through many).它在 HTML 文档中搜索 ID 等于“某物”的元素(如果超过 1,将进行第一次迭代,但您可以循环遍历多个)。 It will then get take the table data tag and go to the iteration (3) where the text is (0 would equal the first TD).然后它将获取表数据标记并转到文本所在的迭代 (3)(0 将等于第一个 TD)。

Let us know if this works.让我们知道这是否有效。

Many HTML documents have names in generalized tables as opposed to ID's许多 HTML 文档在通用表中都有名称,而不是 ID

I commonly use some form of what is shown below;我通常使用如下所示的某种形式;

Set HTML = IE.Document
Set SomethingID = HTML.GetElementByID("something").Children
For Each TR in SomethingID
   If TR.TagName = "td" Then
      If TR.Name = "myname" Then
         'code in here if you are looking for a name to the element
      ElseIf TR.Value = "myValue" Then
         'Code in here if you are looking for a specific value
      ElseIf TR.innerText = "mytext" Then
         'more code in here if you are looking for a specific inner text
      End If
   End If
Next TR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM