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
Avatar of Seware Kangalu
Seware Kangalu

ASKER

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
Avatar of Qlemo
Qlemo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Seware Kangalu

ASKER

Thank you for such helpful suggestions!
VBA
VBA

Visual Basic for Applications (VBA) enables building user-defined functions (UDFs), automating processes and accessing Windows API and other low-level functionality through dynamic-link libraries (DLLs). VBA is closely related to Visual Basic and uses the Visual Basic Runtime Library, but it can normally only run code within a host application rather than as a standalone program. It can, however, be used to control one application from another via OLE Automation. VBA is built into most Microsoft Office applications.

17K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo