EnginerdUNH
New Member
- Joined
- Aug 14, 2019
- Messages
- 2
- Office Version
- 365
- Platform
- Windows
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?