Avatar of Seware Kangalu
Seware Kangalu asked on

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

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

VBA

Avatar of undefined
Last Comment
Seware Kangalu

8/22/2022 - Mon
ASKER
Seware Kangalu

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?
ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Seware Kangalu

Thank you for such helpful suggestions!
Your help has saved me hundreds of hours of internet surfing.
fblack61