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(eleme
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
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"