I'm working on some code in VB.NET which will do some things automatically. I have a couple of buttons which will do some actions manually. But I also have one button that does some actions automatically.
If I press the buttons to run it manually, it works fine.
But when I press the button to run it automatically, it gives me an Object reference not set to an instance of an object exception.
This is the code that gives the exception:
Private Sub btnStep4_Click(sender As Object, e As EventArgs) Handles btnStep4.Click
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
Dim ran As Boolean = False
For Each webpageelement As HtmlElement In allelements
WebBrowser1.Document.GetElementById("website_name").SetAttribute("value", txtWebName.Text + txtWebExt.Text) '< This one
WebBrowser1.Document.GetElementById("website_code").SetAttribute("value", txtWebName.Text) '< And this one
If webpageelement.GetAttribute("title") = "Website opslaan" And ran = False Then
webpageelement.InvokeMember("click")
ran = True
End If
Next
End Sub
So when I click the btnStep4 button, it works. But when I simulate the click like this:
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
btnStep4_Click(sender, e)
I get the exception.
Why is this? Is there something I'm missing?
This might be a stupid mistake, but I just can't find out what I'm doing wrong.
Any help or tips would be greatly appreciated!