Maria
Maria

Reputation: 3

Extract data out of HTML table cell

I want to extract an information from a website (non-public intranet) with VBScript. It is a time which is always in the same field on the website, but each date different and afterwards I want to create with this information a meeting in my outlook (duration always exactly 1 hour) at exactly this time at the current day. I am fine with opening the website with VBScript and putting the meeting, but I have problems to read out the time information of the website field.

Unfortunately I cannot paste the picture of the full structure here, so I paste you the DOM element of the information I am looking for:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML Strict//EN"><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<HTML><BODY id="info_body" aLink="#ff0000" link="#0000ff" bgColor="#ebf7ff" text="#000000" vLink="#800080"><TABLE class="topMargin" border="0" cellSpacing="2" cellPadding="2" width="600" align="center"><TBODY>    <TR class="data">

<TD>
11:42
</TD>

</TR></TBODY></TABLE></BODY></HTML>

Is this helping you? I would need to set the information (in this case "11:42") to a variant and/or to show it in a Msgbox. And the structure is:

html --> body --> iframe (3rd) --> html --> body --> table (1st) --> tbody
--> tr class = "data" --> td (4th)

For opening the website I use:

SET ie1 = WScript.CreateObject("InternetExplorer.Application", "IE_")
myUrl1="http://xxxx" 'website
hwnd = ie1.hwnd
ie1.Navigate myUrl1

Set oShell = CreateObject("Shell.Application")
For Each Wnd In oShell.Windows
  If hwnd = Wnd.hwnd Then Set ie1 = Wnd
Next 

For getting the required information I tried:

  1. MsgBox ie.document.body.innerhtml

    which works and gives me the full html code.

  2. getElementsByTagName

  3. firstChild
  4. ie.document.childNodes(0).childNodes(1).childNodes(0).childNodes(0).childNodes(2).childNodes(3)

I found these code snippets in the internet and apparently they work for other, but I am sorry, I am not able to adapt to my specific requirements and case.

UPDATE 27.05.2015:

With this new versions it works most of the times, but unfortunately not always...

Option Explicit

Dim ie, b, url
Dim hwnd, oshell, wnd
Dim tbl, iframe, td
Dim c, Zeit1, start_punkt
Dim outl, a, myNameSpace, myFolder, myitem, alle_items, myitem_new, olmeeting
Dim ie_exist

URL = "http://xxxx" 'website

SET ie = WScript.CreateObject("InternetExplorer.Application", "IE_")
hwnd = ie.hwnd
ie.Navigate url

Set oShell = CreateObject("Shell.Application")
For Each Wnd In oShell.Windows
If hwnd = Wnd.hwnd Then Set ie = Wnd
Next

DO WHILE ie.ReadyState <> 4
LOOP

'get 3rd iframe in page
Set iframe = ie.document.getElementsByTagName("iframe").Item(2).contentWindow

'get 1st table in iframe
Set tbl = iframe.document.getElementsByTagName("table").Item(0)

'get 4th cell in table
Set td = tbl.getElementsByTagName("td").Item(8)

Msgbox td.innerText

When it is not working I get an unkown error message in line:

If hwnd = wnd.hwnd then set ie = Wnd

@Ansgar: If I remove the Set oShell = ... part I get the error message:

The object invoked has disconnected from its clients.

And if I remove the Set oShell = ... part as well as the DO WHILE ie.ReadyState loop, I get unkown error in line:

Set iframe = ie.document.getElementsByTagName("iframe").Item(2).contentWindow

Any ideas? At least the version above works most of the times (the tried alternatives result each time in an error), but is there any possibility to fix the problem and make it working EACH time?

Upvotes: 0

Views: 4976

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Something like this should do:

url = "http://www.example.com/"

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate url

While ie.ReadyState <> 4
  WScript.Sleep 100
Wend

'get 3rd iframe in page
Set iframe = ie.document.getElementsByTagName("iframe").Item(2).contentWindow
'get 1st table in iframe
Set tbl = iframe.document.getElementsByTagName("table").Item(0)
'get 4th cell in table
Set td  = tbl.getElementsByTagName("td").Item(3)

MsgBox td.innerText

BTW, this code is pointless, so drop it:

Set oShell = CreateObject("Shell.Application")
For Each Wnd In oShell.Windows
  If hwnd = Wnd.hwnd Then Set ie1 = Wnd
Next

You don't need to re-attach to an Internet Explorer instance when you already have a reference to that instance.

Upvotes: 1

Related Questions