hiren
Free And Full Download Latest Software,Project,E-Book,Mobile Application,Movies,MP3 Song,Game And Latest Trick in www.123ProjectPlus.co.cc

Search Google

Loading

Thursday, 2 June 2011

Successfully read HTML elements from the WebBrowser control

At times, you may want to read text or other HTML from the contents of the WebBrowser control. To this end, you can simply place some kind of collection code in the control's DocumentComplete() event. As you may know, the WebBrowser fires this event when it has successfully downloaded the Web page in question. With this in mind, the code might look as follows:

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Label1.Caption = WebBrowser1.Document.All("myElem").innerHTML
End Sub

Keep in mind, however, that this code only works for Web pages that don't contain frames. Since the DocumentComplete() event fires once for each frame, the original code might actually try to access a document element that doesn't yet exist (hasn't been rendered in the browser).

To work around this potential pitfall, you can use a conditional to test the pDisp object against the WebBrowser control itself. With multi-frame documents, the WebBrowser fires the DocumentComplete() event one last time after it has loaded all the necessary frames. With this in mind, the following code would accommodate this behavior and fill the Label with the appropriate text under all circumstances.

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is WebBrowser1.object) Then
Label1.Caption = WebBrowser1.Document.All("myElem").innerHTML
End If
End Sub

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls