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?
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
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
Last edited by jmsrickland; May 28th, 2016 at 12:13 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
'
' 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
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
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.