I am trying to get the results of tracking a package on my screen. In Excel, I have this code, which successfully opens the IE browser and successfully types the tracking number into the input box. What doesn't work is the last step - button click. I get the runtime error 91 - object variable or with block variable not set. Thanks in advance.
Sub TrackUSPS()
Const cURL = "https://tools.usps.com/go/TrackConfirmAction!input.action" 'Enter the web address here
Const trackNum = "9405511899560005266920" 'Tracking number
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim TrackInputBox As HTMLInputElement
Dim TrackButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Dim qt As QueryTable
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate cURL
'Wait for initial page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc = IE.document
'Get the tracking form on the page
Set LoginForm = doc.forms(1)
'Get the tracking input box and populate it
'<input type="text" id="search-text" autocomplete="off" name="searchText" class="default" value="Search USPS.com or Track Packages" onclick="javascript:dojo.byId('search-text').value='';"/>
Set TrackInputBox = LoginForm.elements("search-text")
TrackInputBox.Value = trackNum
'Get the form input button and click it
'<input type="image" id="search-btn" src="/media/images/global/blank.gif" alt="Process Search" />
Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
This works for me, replace Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
with
IE.document.getelementbyID("search-btn").Click
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.