(Solved) – Error 91 in VBA when variables are already defined and set

  • by

Hi there i’m a newbie at coding and this is my first post on stackoverflow.
I’m trying to automate a repetitive task at my work.
So it’s a matter to login, then deleting a text field(where the “0000” should be, just for example), then pasting another value from Excel in another field, clicking on search and scrape some data.

 HTMLInput.Value = "0000000"

Here above is where i get the runetime error 91[Object variable or With not set], and i really don’t get why is this happening. I thought that every variable is defined.
Or am i missing something after the login, like something else should happen to ‘HTMLInput’?

Option Explicit

Sub LogIn()

    Dim IE As New SHDocVw.InternetExplorer
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLInput As MSHTML.IHTMLElement

    IE.Visible = True

    IE.Navigate Sheet1.Range("B2").Text

    Do While IE.ReadyState <> READYSTATE_COMPLETE

    Set HTMLDoc = IE.Document

    'Setting Username to a specific value
    Set HTMLInput = HTMLDoc.getElementById("ctl00_WebPartManager1_gwpLogin1_Login1_UserName")
    HTMLInput.Value = Sheet1.Range("B3")

    'Setting Password to a specific value
    Set HTMLInput = HTMLDoc.getElementById("ctl00_WebPartManager1_gwpLogin1_Login1_Password")
    HTMLInput.Value = Sheet1.Range("B4")

    'Click to login
    Set HTMLInput = HTMLDoc.getElementById("ctl00_WebPartManager1_gwpLogin1_Login1_LoginButton")


    Set HTMLInput = HTMLDoc.getElementById("ctl00_SearchSection_ObjectSearch_txtEMail")

    HTMLInput.Value = "0000000"

    Set HTMLInput = HTMLDoc.getElementById("ctl00_SearchSection_ObjectSearch_txtCustomerID")
    HTMLInput.Value = Sheet1.Range("A10")

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *