Zacariah171
New Member
- Joined
- Apr 2, 2019
- Messages
- 28
I'm using Excel to log in to a website, enter the serial number of a device into a search field and hit Enter (there is no "button" to press), which takes me to the next page, and then I need to click on a button with the ID "inventoryNewInspectionButton". The website requires a login but I'm typically always logged in, so I didn't worry about adding anything in here for that. When I run the macro, my variable HTMLButton will return as = Nothing. However, when I use the Step Into feature, the macro works just fine. What am I doing wrong? Any help is much appreciated!
Sub BeginInspection()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim frm As MSHTML.HTMLFormElement
IE.Visible = True
IE.Navigate "ourwebsitehere"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
'Enter the serial number and hit Enter
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("searchText")
HTMLInput.Value = "Serial-068"
Set frm = HTMLInput.form
frm.submit
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLButton = HTMLDoc.getElementById("inventoryNewInspectionButton") 'When I run the macro, HTMLButton = Nothing, so the next line fails. When I use Step Into, then HTMLButton = "[objectHTMLButtonElement]" and the HTMLButton.Click works.
HTMLButton.Click
This is from the website for the button I'm needing to press.
<button class="red_button" id="inventoryNewInspectionButton" type="button">
<span class="button_text">Begin Inspection</span>
<img class="button_icon" alt="" src="/resources/images/icons/menu_white.png">
</button>
Sub BeginInspection()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim frm As MSHTML.HTMLFormElement
IE.Visible = True
IE.Navigate "ourwebsitehere"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
'Enter the serial number and hit Enter
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("searchText")
HTMLInput.Value = "Serial-068"
Set frm = HTMLInput.form
frm.submit
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLButton = HTMLDoc.getElementById("inventoryNewInspectionButton") 'When I run the macro, HTMLButton = Nothing, so the next line fails. When I use Step Into, then HTMLButton = "[objectHTMLButtonElement]" and the HTMLButton.Click works.
HTMLButton.Click
This is from the website for the button I'm needing to press.
<button class="red_button" id="inventoryNewInspectionButton" type="button">
<span class="button_text">Begin Inspection</span>
<img class="button_icon" alt="" src="/resources/images/icons/menu_white.png">
</button>