1

My goal is to create a VB application that reads and writes information to various webpages loaded in Internet explorer.

I have created a program that works exactly as I intend in VBA. I am now trying to re-implement the same programming in VB.

I have a function that looks for and returns an Internet Explorer Object where the input matches the LocationName.

Assuming the target page is loaded, I can work with it. Methods such as getElementByID() work perfectly. If the browser window is closed and reopened, and the code is run again, the results are very inconsistent. The getIE function seems to work, but when trying to use methods like document.getElementByID() a NullReferenceException is thrown.

Does anyone know if there is anything I am missing that I need to include to get the document property to update?

EDIT: I have looked over the NullReferenceException article. It hasn't helped me unfortunately. In case I was unclear in my wording, I would like to reiterate that the same bit of code yields a different result when executed the 2nd time under the same conditions (same webpage open in Internet Explorer).

On the second execution IE.locationName is retrievable but IE.Document.title is not. The problem is definitely with the Document property as far as I can tell. I am truly stumped.

Many thanks

Public Function getIE(targetTitle As String) As SHDocVw.InternetExplorer

    ' Create the shell application and Collection of open windows
    Dim shellObj As Object = CreateObject("Shell.Application")
    Dim shellWindows As SHDocVw.ShellWindows = shellObj.Windows()
    getIE = Nothing

    ' Scan through the Collection
    For I = (shellObj.Windows.Count - 1) To 0 Step -1
        ' If found, assign this to the function output and exit early
        If InStr(shellWindows(I).LocationName, targetTitle) Then
            getIE = shellWindows(I)
            Debug.Print("Found: " & shellWindows(I).LocationName)
            Exit For
        End If
    Next I

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim IE As SHDocVw.InternetExplorer
    IE = getIE("my site title")

    If IE Is Nothing Then
        Debug.Print("Site not open")
        Exit Sub
    End If

    ' The code always gets this far, and despite the page being found, starts throwing exceptions from here on if the window has been closed and reopened whilst my application has stayed running.

    ' Sample form data insertion code
    IE.Document.getElementById("textbox1").value = "my value"
    ' Click the submit button
    IE.Document.getElementById("submit").click()

    ' Wait for page to load
    While IE.Busy
    End While

    IE.Document.getElementById("textbox2").value = "my 2nd value"

    ' done
    IE = Nothing

End sub
  • the element does not exist – Fᴀʀʜᴀɴ Aɴᴀᴍ Aug 18 '15 at 18:17
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Aug 18 '15 at 18:20
  • I would inspect what the document actually contains when you have the error, something like `OuterXML` property. Not sure if that's the actual property, but there is something like that (ToString maybe?) that will dump the HTML of the document for you to inspect. – Thraka Aug 18 '15 at 18:28
  • I am slightly confused by your wording. Do you mean that the program is running and never stops at all during the browser closing and reopening? – Resistance Aug 18 '15 at 18:48
  • Hi Travis I run both the application and load the webpage. I hit button1. The code executes fine. The value is inserted in to textbox 1, the submit button is pressed, and on the newly loaded page a value is inserted in to textbox two. I keep my application open, but close the web browser. I reopen the webbrowser to the exact same starting URL, and retry the process. At the second try of clicking button1, the code falls down when trying to insert the value in to textbox1. –  Aug 18 '15 at 18:54

0 Answers0