Question .NET WebBrowser control's DocumentCompleted event passes no pDisp argument

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
Hello.

I have some experience developing for VBA inside Office apps, but I am quite new to VB.NET, and I just noticed that its WebBrowser control is a bit different from that one I used to reference via ShDocVw to use in my VBA Forms.

ShDocVw.WebBrowser had the DocumentComplete event, which passed these arguments: URL as string and pDisp as object.

.NET WebBrowser has the DocumentCompleted event, in which i successfully found e.URL.ToString, but I couldn't find nothing similar to the pDisp object.

This is critical to me on migrating my project from VBA to VB.NET, for I need to handle particular IFrames when they complete loading, which I only could do by getting pDisp's document property.

So I need help on learning how to do the same thing with this .NET WebBrowser control, and/or how to reference and use good ole ShDocVw.WebBroser in my Visual Studio project, to embed it in my forms.

Thank you very much.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
It might help if you actually told us what 'pDisp' actually is.

The .NET WebBrowser control is actually just a wrapper for the old ActiveX web browser you're talking about. You can still use that ActiveX control but it's generally better not to, because the .NET control generally provides a nicer interface for .NET code. If that interface is deficient in any way for what you need to do, you can still get access to the underlying ActiveX control when you need to. To do that, get the ActiveXInstance property and cast it as the appropriate type. To do that, you need to have referenced the appropriate library. There are two ways to do that:

1. Add a reference directly to your project for Microsoft Internet Controls.
2. Add the ActiveX web browser control to your Toolbox, add an instance to your form and then remove it.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
Private Sub DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

Sender is the 'same' as pDisp
Yes, 'sender' parameter is always the instance of the class that raised the event, ie here it is the WebBrowser instance.
and No, axbrowser pDisp returns different 'browser' objects for each window (frame), while .Net browser sender is always the control on form.
The only link I can see to the frame is the Navigating event, that reveals the e.TargetFrameName and e.Url, so it would be possible to utilize this information with e.Url in DocumentCompleted event.
 

acroes

Member
Joined
Aug 19, 2011
Messages
8
Location
The Netherlands
Programming Experience
5-10
Perhaps the code underneath could help you a hand, I don't have an example of a iframe page nearby, so it's untested.

VB.NET:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] WebBrowser1_DocumentCompleted1(sender [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], e [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] System.Windows.Forms.[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af]WebBrowserDocumentCompletedEventArgs[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]) [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] WebBrowser1.DocumentCompleted
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] url [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] = e.Url.ToString()
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] browser [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af]WebBrowser[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2](sender, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af]WebBrowser[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (url.StartsWith([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"http://"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]) [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] url.StartsWith([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"https://"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]))) [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]' in AJAX 
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (e.Url.AbsolutePath <> [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].WebBrowser1.Url.AbsolutePath) [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]'iframe
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]'real site
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] iframe [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af]HtmlElement[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].WebBrowser1.Document.GetElementsByTagName([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"iframe"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af]Debug[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].WriteLine(iframe.Name)
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af][FONT=Courier New][SIZE=2][COLOR=#2b91af]Debug[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].WriteLine(iframe.OuterHtml)
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]rdy = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]True
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
 
Last edited:

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
Thank you all, I will keep on trying

Hello,

First, thank you all for your kind attention to my problem.

I am quite a rookie in VB.NET, in spite of some years of developing some complex mini-apps inside excel, using VBA. Now I am haunted trying to migrate one of that to VB.NET.

Answering to jmcilhinney, pDisp is a specific WebBrowser object passed as parameter by the DocumentComplete event, whose document is that loaded by an iframe, whenever an iframe loads itself and raises the event. All that is about activex webbrowser control which I use in VBA, ShDocVw.WebBrowser, that lays into ieframe.dll

Until now, my best try was to dim withevents an internet explorer session, to handle it in similar ways I did in VBA.

Of course, I am sorry if I am doing some stupid things, for I am really new to VB.NET, and to Visual Studio IDE as well.

Lots of thanks!
 

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
Did my post help you on the way, or are you still struggeling?

If so, could you post what you have so far, concerning you problem.

I tried exactly what you posted, but the result was not exactly what I need. It correctly found the iframes and got it's outerHTML. It looped, however, through all iframes for each iframe it loaded, so I've got repeated output on immediate window. But that is no problem. The problem is this: I aim to find, retrieve and interact with HTML elements of one document loaded inside one of these iframes. I will try to reproduce, below, what my problem is and what was my approach when doing it in VBA.

Imagine you have a document hosted in http://www.somesite.com/somewebapp/Main.asp which contains something like that:

HTML:
<html>
...lots of scripts...
<body>
...texts and images...
<iframe name=news src=http://www.somesite.com/somewebapp/news.asp>Your browser don't support frames</iframe>
<iframe name=login src=http://www.somesite.com/somewebapp/login.asp>Your browser don't support frames</iframe>
<iframe name=stats src=http://www.somesite.com/somewebapp/stats.asp>Your browser don't support frames</iframe>
...texts and images...
</body>
...lots of scripts...
</html>

I am interested here in "login" iframe, which hosts a document (http://www.somesite.com/somewebapp/login.asp) like that:

HTML:
<html>
<body>
...texts and images...
<input id="user_id" name="user_id">
<input id="password" name="password">
<button onclick="JAVASCRIPT: self.submit();">Submit</button>
...texts and images...
</body>
</html>


Now here's my VBA approach (form's code, where form contains a ShDocVw.WebBrowser control embedded), in order to load first document - doesn't work loading immediately www.somesite.com/somewebapp/login.asp):

VB.NET:
Private Sub Form1_Activate()
     WebBrowser1.Navigate "[U]http://www.somesite.com/somewebapp/Main.asp[/U]"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    Dim oWB as ShDocVw.WebBrowser, oHD as MSHTML.HTMLdocument, oBE as MSHTML.HTMLbuttonElement
    On Error Resume Next
    Set oWB = pDisp: If Not oWB Is Nothing Then If oWB.Type Like "*HTML*" Then Set oHD = oWB.Document
    On Error GoTo 0
    If oHD is Nothing then Exit Sub
    '
    If URL="[U]www.somesite.com/somewebapp/login.asp[/U]" then
         oHD.GetElementsByTagName("INPUT").Item("user_id").Value = "MyUserId"
         oHD.GetElementsByTagName("INPUT").Item("password").Value = "MyPassword"
         For each oBE in OHD.GetElementsByTagName("button")
               If oBE.innerText="Submit" then oBE.Click:Exit For
         Next
    End If
End Sub

Finally, I often use references to ShDocVw, MSHTML, MSXML2, ADODB, etc., all together in my VBA applications. I always did it carelessly and met no big problem. It seems that ascending to VB.NET requires a higher level of consciousness... :-D

I thank you again for your help!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
DocumentCompleted event handler:
If e.Url.ToString = "login.asp" Then
    Dim doc = CType(sender, WebBrowser).Document.Window.Frames("login").Document

End If
 

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
And that makes it! Thank you so much!!

The lesson, it seems, is that I'll have to make without sweet old pDisp, but there are ways to reach the elements I want. All right then! ;-)
 

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
[SOLVED] .NET WebBrowser control's DocumentCompleted event passes no pDisp argument

Now that it is solved, I think I owe posting this conclusion, for the sake and profit of future users with same doubt. That's what I've learned:
  • The control WebBrowser in VB.NET has a DocumentCompleted event which is not entirely similar to the DocumentComplete event from ActiveX control ShDocVw.WebBrowser I always used in VBA;
  • In ShDocVw, DocumentComplete event passes "pDisp" argument, which seems to be a reference to the especific instance of WebBrowser handling the very document that raises the event;
  • In VB.NET, instead, DocumentCompleted passes "sender" argument, which is a reference to the whole WebBrowser control itself;
  • VB.NET is also more strict treating classes of objects, so even if pDisp could be directly assigned to a new ShDocVw.WebBrowser, in VB.NET one must do it using CType(object,class) or DirectCast(object,case) - the latter being faster but demanding absolute certainty about object being passed as argument, which have to belong intrinsecally to the same class you are casting it;
  • That causes an issue when navigating through a web page which has frames, for while pDisp's child object "document" will be the source document of each specific iframe, sender's child object document will always be the same parent document, in which iframes are nested;
  • For that reason, when dealing with those child documents, nested inside the parent one as iframes, VB.NET will demand a preliminar code to find and reference the exact frame, and it's document, that one wants to access;
  • That will be easy if parent's document sets a specific name or id property to each <iframe> tag; in other case, one will have to loop through each iframe searching for a particular content in innerHTML or outerHTML strings.
I also found differences between VB.NET HTML DOM objects and those I used in VBA referring the MSHTML library:
  • VB.NET has only one class for the elements of HTML code, "HTMLelement"; MSHTML, instead, had one for each kind of elements (input, img, table, button, etc.);
  • For that reason, one can't use particular methods of each element to start its events; instead, it has to be done through the .RaiseEvent(eventname) method;
  • VB.NET doesn't allow one to get and set element's value through its .Value property; it is required to use .GetAttribute("value") and .SetAttribute("value",newvalue) methods instead.
Enough chatter done, these are my code snippets for doing the same task, either in VBA and in VB.NET:

VBA approach:
VB.NET:
Private Sub Form1_Activate()
     WebBrowser1.Navigate "http://www.somesite.com/somewebapp/Main.asp"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    Dim oWB as ShDocVw.WebBrowser, oHD as MSHTML.HTMLdocument, oBE as MSHTML.HTMLbuttonElement
    On Error Resume Next
    Set oWB = pDisp: If Not oWB Is Nothing Then If oWB.Type Like "*HTML*" Then Set oHD = oWB.Document
    On Error GoTo 0
    If oHD is Nothing then Exit Sub
    '
    If URL="http://www.somesite.com/somewebapp/login.asp" then
         oHD.GetElementsByTagName("INPUT").Item("user_id").Value = "MyUserId"
         oHD.GetElementsByTagName("INPUT").Item("password").Value = "MyPassword"
         For each oBE in OHD.GetElementsByTagName("button")
               If oBE.innerText="Submit" then oBE.Click:Exit For
         Next
    End If
End Sub

VB.NET approach:
Private Sub Form1_Load()
     WebBrowser1.Navigate("http://www.somesite.com/somewebapp/Main.asp")
End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Dim oHD as HTMLdocument

    If e.Url.ToString = "http://www.somesite.com/somewebapp/login.asp" then
         oHD = DirectCast(sender, WebBrowser).Document.Window.Frames("login").Document
         Dim oIEuser_id as HTMLelement = oHD.GetElementsByTagName("INPUT").Item("user_id")
         oIEuser_id.SetAttribute("value","MyUserId")
         Dim oIEpassword as HTMLelement = oHD.GetElementsByTagName("INPUT").Item("password")
         oIEpassword.SetAttribute("value","MyPassword")
         For each oBE in oHD.GetElementsByTagName("button")
               If oBE.innerText="Submit" then oBE.Click : Exit For
         Next
    End If

End Sub


Now, I thank very much to all who helped me learning this and solving my practical issue. Hope I can count on you folks again!
 
Last edited:
Top Bottom