We help IT Professionals succeed at work.

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

Seware Kangalu
on
53 Views
Last Modified: 2019-01-22
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 :

https://app.impact.com

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 = "https://app.impact.com"
    
    '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
    
    Do
        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 = "TEST@Gmail.com"
    
    

End Sub

Open in new window

Comment
Watch Question

Author

Commented:
I also tried to achieve the same thing by re-writing the code a bit and using the following at the location of the error:

IEdoc.forms("login").elements("j_username").Value = affiliateUserName

That did not work and I was still getting the same error. I finally found a workaround by implementing some error handling and running the code again when the error occurs via a loop. Please see below:

Option Explicit

Sub GetHTMLDocument()
    
    'Declare the variable for Internet Explorer
    Dim IE As SHDocVw.InternetExplorer
    Set IE = New InternetExplorer
    Dim myURL As String
    IE.Visible = True
    
    Dim wb As Workbook
    Set wb = ThisWorkbook
    
    Dim affiliatePw As String
    Dim affiliateUserName As String
    
    affiliateUserName = wb.Worksheets(1).Range("impactRadiusUn").Value
    affiliatePw = wb.Worksheets(1).Range("impactRadiusPw").Value

    
    'Impact Radius URL
    myURL = "https://app.impact.com"
    
    '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
    
    Do
        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
        
        Set IEdoc = IE.document
        
        
        elementId = "j_username"
        
        'For some reason error 91 keeps popping up so ignore it and try again
        Do
        On Error Resume Next
        
        IEdoc.forms("login").elements("j_username").Value = affiliateUserName
        IEdoc.forms("login").elements("j_password").Value = affiliatePw
        
        
        Loop Until IEdoc.forms("login").elements("j_username").Value <> "" _
            And IEdoc.forms("login").elements("j_password").Value <> ""

        

End Sub

Open in new window


I don't think this is the best way of dealing with this issue though. Does someone have a better idea that works better than this?
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you for such helpful suggestions!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.