0

AddHandler ie.DocumentCompleted gives DocumentCompleted' is not an event of 'Object'

if I put ie.DocumentComplete I get

Value of type 'WebBrowserDocumentCompletedEventHandler' cannot be converted to 'DWebBrowserEvents2_DocumentCompleteEventHandler'

I followed several examples from the web and keep getting these errors. As if its not a code problem but that I'm missing a reference.

Public Class Form1
    Dim myHTMLDoc As HtmlDocument
    Dim myhtmlelement As HtmlElementCollection
    Dim ie As WebBrowser
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ieurl As String
        ieurl = "http://companyurl.com"
        AddHandler ie.DocumentComplete, AddressOf DocumentCompleteIE
        ie.Navigate(ieurl)
        ie.Visible = True

    End Sub
    Private Sub DocumentCompleteIE(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        MessageBox.Show("completed - " & ie.LocationURL)
        ie.Document.GetElementById("Login").SetAttribute("value", "x123456")
        ie.Document.GetElementById("Pwd").SetAttribute("value", "123456")
        ie.Document.GetElementById("Btn").InvokeMember("click")
    End Sub
End Class

If I start typing ie.Do (after addhandler) I see DocumentComplete as choice and not DocumentCompleted, which I find odd as the code I see everywhere on the net uses DocumentCompleted!

Peter
  • 148
  • 1
  • 2
  • 18
  • `DocumentCompleted` is related to the WebBrowser control. You're using the InternetExplorer ActiveX directly. See its documentation instead. Remove `New WebBrowserDocumentCompletedEventHandler`. – Jimi Aug 12 '19 at 15:04
  • What signifies that I'm using ActiveX directly? The Imports SHDocVw? I see that I probably should use the WebBrowser control directly as the user doesn't need the IE page, just some data on it! – Peter Aug 12 '19 at 15:45
  • Not sure what you meant by remove New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompleteIE)! – Peter Aug 12 '19 at 15:48
  • 1
    1) It means that you're using the [InternetExplorer object](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752084(v=vs.85)), not its managed WebBrowser control wrapper. You're mixing up methods that belogs to two different objects. The code you posted is just an attempt to auto-login, filling in and activating a WebForm. Use the WebBrowser control for this: much simpler, less baggage, same result. 2) `AddHandler [WebBrowser].DocumentCompleted, AddressOf DocumentCompleteIE` – Jimi Aug 12 '19 at 15:57
  • Now I get this error on DocumentComplete $exception {"Object reference not set to an instance of an object."} System.NullReferenceException – Peter Aug 12 '19 at 16:15
  • Changed the code to this! [code]Dim ie As WebBrowser 'Public Event DocumentCompleted As WebBrowserDocumentCompletedEventHandler Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ieurl As String ieurl = "http://companyurl.com" AddHandler ie.DocumentCompleted, AddressOf DocumentCompleteIE ie.Navigate(ieurl) ie.Visible = True End Sub Private Sub DocumentCompleteIE(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)[/code] – Peter Aug 12 '19 at 16:17
  • Please, don't add code in comments. Update your question if you have altered your code. Remove this thing: `Public Event DocumentCompleted As WebBrowserDocumentCompletedEventHandler`, it makes no sense. See [Auto Website Login: SetAttribute not working](https://stackoverflow.com/a/54030367/7444103). – Jimi Aug 12 '19 at 16:20
  • Still getting eroor System.NullReferenceException: 'Object reference not set to an instance of an object.' on addhandler DocumentCompleteIE. My code matches the code from your suggested url! ( I think!) – Peter Aug 12 '19 at 16:37
  • 1
    `ie = New WebBrowser`. This: `Dim ie As WebBrowser` defines a Type, it doesn't create the instance. – Jimi Aug 12 '19 at 16:39

1 Answers1

0

If you want to use web browser control to load web page and fill the information and click the button than you can refer an example below.

Public Class Form1
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'this is to load the site in web browser control.
        Me.WebBrowser1.Navigate("http://localhost/login_form.html")
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' this is to enter values in the textboxes and click the button in web browser control.
        Dim usernameTextBox As HtmlElement = Me.WebBrowser1.Document.All.Item("uname")
        usernameTextBox.InnerText = "x123456"

        Dim passwordTextBox As HtmlElement = Me.WebBrowser1.Document.All.Item("psw")
        passwordTextBox.InnerText = "123456"

        Me.WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
    End Sub


End Class

Output:

enter image description here

You can also use WebBrowser1_DocumentCompleted event to enter the values. I had also tested it and it is working fine. To use that you can simply copy the code from Button3_Click event and paste it in WebBrowser1_DocumentCompleted event.

If you want to open IE browser and perform the same operation as above than you can refer code example below.

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'this is to open IE browser and load the page and entering the values and click the button.
        Dim TheBrowser As Object = CreateObject("InternetExplorer.Application")
        TheBrowser.Visible = True
        TheBrowser.Navigate("http://localhost/login_form.html")
        Do Until TheBrowser.ReadyState = 4 'PAGE_LOADED
        Loop
        TheBrowser.Document.GetElementById("uname").SetAttribute("value", "x123456")
        TheBrowser.Document.GetElementById("psw").SetAttribute("value", "123456")
        TheBrowser.Document.GetElementById("signIn").InvokeMember("click")
    End Sub
End Class
Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19