Object variable or With block not set error when trying to fill in input fields on a website

EnginerdUNH

New Member
Joined
Aug 14, 2019
Messages
1
Hi, I am writing the following code which allows me to proceed to a website that I need to gather info from to then export into excel. I am very new to HTML web scraping with VBA and I am currently writing it line by line and testing it as I go.
Code:
[FONT=Verdana]Sub GoToMarketingScents()
    'navigate to marketing scents
    Dim IE As New SHDocVw.InternetExplorer
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLAs As MSHTML.IHTMLElementCollection
    Dim HTMLA As MSHTML.IHTMLElement
    Dim HTMLUser As MSHTML.IHTMLElement
    Dim HTMLPWord As MSHTML.IHTMLElement[/FONT]
[FONT=Verdana]    IE.Visible = True
    IE.navigate "http://marketingscents.com/"
    
    Do While IE.readyState <> READYSTATE_COMPLETE
        
    Loop
    
    'navigate to login page
    Set HTMLDoc = IE.document
    
    Set HTMLAs = HTMLDoc.getElementsByTagName("a")
    
    For Each HTMLA In HTMLAs
        'Debug.Print HTMLA.getAttribute("classname"), HTMLA.getAttribute("href")
        If HTMLA.getAttribute("classname") = "header__button button button--secondary button--large button--login" And HTMLA.getAttribute("href") = "https://www.marketingscents.com/index?page=login-new" Then
            HTMLA.Click
            Exit For
        End If
    Next HTMLA
    
    Do While IE.readyState <> READYSTATE_COMPLETE
        
    Loop
    
    'Prompt user to enter in their login info and login to website
    Set HTMLUser = HTMLDoc.getElementById("form-email")
    HTMLUser.Value = "xxxxxxx"
    Set HTMLPWord = HTMLDoc.getElementById("form-password")
    HTMLPWord.Value = "************"
    
End Sub
[/FONT]
Currently I am at the point where I need to enter my user login information and I have looked at the website's HTML code and know the fields that I need to populate. My problem is that when I get to the line that defines what the username should be (HTMLUser.Value) I get an "Object variable or With block not set" error. However, when I attempt to step into the code line by line using F8 I get no errors at all and the code works exactly as required. Can anyone tell me what I need to add in order to get this code to also work when I run the entire macro?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Watch MrExcel Video

Forum statistics

Threads
1,129,803
Messages
5,638,448
Members
417,025
Latest member
MusterDuster

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top