Code breaks when it is ran but works when using Step Into

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>
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Sorry I am a bit busy, but in general
I suspect it faces error during the loop
When you step into you do only 1 loop, but error can occur on 2, 3, 4 e.t.c loop.
 
Upvote 0
I tried without the loop and it still does the same thing. If I change the original code so that I go directly to the correct url, it works fine.

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

IE.Visible = True
IE.Navigate "https://ourwebsite/Inventory/Inventory/InventoryDetails/171" 'The problem with this is that I won't know the correct url.

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.Document

'Click "Begin Inspection" button
Set HTMLButton = HTMLDoc.getElementById("inventoryNewInspectionButton")
HTMLButton.Click


However, I don't know what the url will be; that's determined by the serial number that I have to enter. So I can't use that solution. It seems like what's breaking down in my original code is that after I submit the form and I get taken to a new page, then the Set HTMLButton=..... line doesn't know that I'm on the new page and it's trying to find .getElementById("inventoryNewInspectionButton") on the first page. Could that be what's happening?

In the original code, if I have the code as below, then I'll get a Run-time error 91: Object variable or With block variable not set and the debugger will highlight the HTMLButton.Click line and HTMLButton = Nothing. If I move back to the Set HTMLButton =..... line and use Step Into, then HTMLButton gets set to HTMLButton = "[objectHTMLButtonElement]" like I would have expected to in the first place. Why is this not getting set when I run it but does get set when I use Step Into?
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 "https://ourwebsite/Home/Index"

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("searchText")
HTMLInput.Value = "ML-068"
Set frm = HTMLInput.form
frm.submit

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLButton = HTMLDoc.getElementById("inventoryNewInspectionButton")
HTMLButton.Click


If I change to the code below, then I'll get a Run-time error 424: Object required and the debugger will highlight the Set HTMLButton = .... line. If I use Step Into, then HTMLButton gets set to HTMLButton="[objectHTMLButtonElement]" like I would have expected in the first place. What am I doing wrong?
Set HTMLButton = IE.Document.getElementById("inventoryNewInspectionButton")
HTMLButton.Click


I'm not sure if that information helps or just makes things more confusing but I figured I'd share it anyway.
 
Upvote 0
Update. I got this to work on my own by adding Application.Wait. I posted the entire code below. I hope this helps anybody with a similar issue.

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 InspButton As MSHTML.IHTMLElement
Dim SaveInspButton As MSHTML.IHTMLElement
Dim frm As MSHTML.HTMLFormElement
Dim Inv1 As String

IE.Visible = True
IE.Navigate "https://ourwebsite.com/Home/Index"

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

'Use the serial number from the Excel worksheet, enter it into the web page, and hit Enter
Inv1 = Range("A1").Value
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("searchText")
HTMLInput.Value = Inv1
Set frm = HTMLInput.form
frm.submit

'Wait for next page to fully load
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now + TimeValue("0:00:5")) 'Added this in and it got the code to work when I ran it.

'Click button
Set HTMLButton = HTMLDoc.getElementById("inventoryNewInspectionButton")
DoEvents 'It seemed like I had to add this in here for this section to work.
HTMLButton.Click

'Wait for the next window to fully load
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now + TimeValue("0:00:5"))

'Click the Save button
Set SaveInspButton = HTMLDoc.getElementById("SaveInspectionButton")
DoEvents
SaveInspButton.Click

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

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
Back
Top