Results 1 to 10 of 10

Thread: WebBrowser - Extract SPAN ID= value

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Location
    Brisbane
    Posts
    19

    Question 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>&nbsp;
    <span id="lblTotal" class="omsReadOnlyAlignLeft">10</span>
    </TD>
    </TR>

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    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>&nbsp;<span id='lblTotal' class='omsReadOnlyAlignLeft'>10</span></TD></TR>"
        TextBox1.Text = Doc.getElementById("lblTotal").innerHTML
    Last edited by DEXWERX; May 27th, 2016 at 01:45 PM.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: WebBrowser - Extract SPAN ID= value

    Assuming a WebBrowser control:

    Name:  sshot.png
Views: 2046
Size:  13.0 KB

    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>&nbsp;" _
                            & "<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
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
    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.

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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


    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.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: WebBrowser - Extract SPAN ID= value

    Quote Originally Posted by jmsrickland View Post
    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.
    Attached Files Attached Files

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: WebBrowser - Extract SPAN ID= value

    That one works


    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.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Location
    Brisbane
    Posts
    19

    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.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Location
    Brisbane
    Posts
    19

    Re: WebBrowser - Extract SPAN ID= value

    Thanks again for everyone's help. Much appreciated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width