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