troubleshooting Question

Run-time error 91 because Set HTMLInput = Nothing. Please help!

Avatar of Seware Kangalu
Seware Kangalu asked on
3 Comments1 Solution60 ViewsLast Modified:
Hi guys,

I had this question after viewing Issue in VBA "getElementById".

My question is very similar. When I run the following code, sometimes it randomly works and inputs "Test" where the "Username" should be. However, 90% of the time, it gives the Run-time error '91': Object variable or With block variable not set. I don't understand why... I have also attached a screen shot of the inspected element. The url I'm visiting is :

I've been able to determine that the issue happens at the following location:

Set HTMLInput = IEdoc.getElementById(elementId)

When the above code is run, HTMLInput = Nothing for some reason.

However, when I put a breakpoint at the error point, then run the code and I press F8 to step into the code once it hits the breakpoint, then the code seems to work as it should and the value "TEST" is typed into the Username box.

Please see the code below.. why am I getting this error and how do I fix it within the code so that it runs without the breakpoint and stepping into it?:

Private Sub pullData_Click()
    'Declare the variable for Internet Explorer
    Dim IE As SHDocVw.InternetExplorer
    Set IE = New InternetExplorer
    Dim myURL As String
    IE.Visible = True
    'Impact Radius URL
    myURL = ""
    'set the username variable to be put into the website
    Dim impactRadiusUserName As String
    impactRadiusUserName = Sheet1.Range("impactRadiusUn").Value
    'Go to the URL
    IE.navigate myURL
        Loop Until IE.readyState = READYSTATE_COMPLETE
        'This code allows to look through the HTML code in the website document
        'for the variables (username and password)
        Dim IEdoc As MSHTML.HTMLDocument
        Dim elementId As String
        Dim HTMLInput As MSHTML.IHTMLElement
        Set IEdoc = IE.document
        elementId = "j_username"
       ' IEdoc.forms("login").elements("j_username").Value = "TEST"
        'Getting the element by ID so we can input the username
        Set HTMLInput = IEdoc.getElementById(elementId)
        HTMLInput.Value = ""

End Sub
"Batchelor", Developer and EE Topic Advisor
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros