WebBrowser - Extract SPAN ID= value
Hi,
I need to extract the SPAN ID (lblTotal - The number '10' in the HTML below) value from the following HTML and display it in a Textbox in VB6. Can some show me how to make that happen, please?
Thanks.
<TR>
<TD align="right" colspan="6">
<span id="Label1">Total</span>
<span id="lblTotal" class="omsReadOnlyAlignLeft">10</span>
</TD>
</TR>
Re: WebBrowser - Extract SPAN ID= value
TextBox1.Text = WebBrowser1.Document.getElementById("lblTotal").innerHTML
Or if you're not using a webbrowser.
Code:
Dim Doc As New MSHTML.HTMLDocument
Doc.body.innerHTML = "<TR><TD align='right' colspan=6><span id='Label1'>Total</span> <span id='lblTotal' class='omsReadOnlyAlignLeft'>10</span></TD></TR>"
TextBox1.Text = Doc.getElementById("lblTotal").innerHTML
2 Attachment(s)
Re: WebBrowser - Extract SPAN ID= value
Assuming a WebBrowser control:
Code:
Option Explicit
Private SuppressEvents As Boolean 'Stop "Click" action for List1.AddItem
Private SpanElements As MSHTML.IHTMLElementCollection
Private Sub Form_Load()
WebBrowser1.Navigate "about:blank" 'We'll stuff in some HTML when ready.
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
With Frame1
.Move 60, ScaleHeight - .Height - 60, ScaleWidth - 120
List1.Height = .Top - 60
End With
With List1
WebBrowser1.Move .Width, 0, ScaleWidth - .Width, .Height
End With
End If
End Sub
Private Sub List1_Click()
Dim SpanElement As MSHTML.HTMLSpanElement
If SuppressEvents Then Exit Sub
With List1
Set SpanElement = SpanElements.Item(.List(.ListIndex))
End With
With SpanElement
Text1.Text = .id
Text2.Text = .textContent
End With
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Doc As MSHTML.HTMLDocument
Dim SpanElement As MSHTML.HTMLSpanElement
Set Doc = WebBrowser1.Document
With Doc
.body.innerHTML = "<table><tr><td>" _
& "<span id='Label1'>Total</span> " _
& "<span id='lblTotal' style='Color: red'>10</span>" _
& "</td></tr></table>"
Set SpanElements = .All.tags("span")
SuppressEvents = True
For Each SpanElement In SpanElements
List1.AddItem SpanElement.id
Next
SuppressEvents = False
End With
End Sub
Re: WebBrowser - Extract SPAN ID= value
Object doesn't support this property or method
Code:
Private Sub List1_Click()
Dim SpanElement As MSHTML.HTMLSpanElement
If SuppressEvents Then Exit Sub
With List1
Set SpanElement = SpanElements.Item(.List(.ListIndex))
End With
With SpanElement
Text1.Text = .Id
Text2.Text = .textContent
End With
End Sub
Re: WebBrowser - Extract SPAN ID= value
Code:
'
' NOTE: Set a reference to Microsoft HTML Object Library
'
Dim spanElement As MSHTML.HTMLSpanElement
For Each spanElement In WebBrowser1.Document.getElementsByTagName("span")
If spanElement.Id = "lblTotal" Then
Text1.Text = spanElement.innerText
Exit For
End If
Next spanElement
End Sub
1 Attachment(s)
Re: WebBrowser - Extract SPAN ID= value
Quote:
Originally Posted by
jmsrickland
Object doesn't support this property or method
Wow, that's a weird one!
It appears to be an issue with the IE version. Try this version that falls back on the more generic HTMLHtmlElement. It seems to work here on WinXP SP3, Vista SP2, and Win7 SP1.
Re: WebBrowser - Extract SPAN ID= value
Re: WebBrowser - Extract SPAN ID= value
Thanks for the replies.
I should have explained a bit better ... the HTML above is contained within a HTML webpage that my app opens and updates every 60 seconds. i.e intranet.mywebsite.com/totals.html
The SPAN ID= HTML is contained within the webpage, so I need to capture the lblTotal and display it every time it updates.
Re: WebBrowser - Extract SPAN ID= value
Well you should have enough information to handle that now. Just do what you need every time a new DocumentComplete event is raised.
Re: WebBrowser - Extract SPAN ID= value
Thanks again for everyone's help. Much appreciated.