I want to have a record of parcels having tracking numbers in excel column A and details of the available field on other columns so that whenever I press the button to run module it updates me about the parcel details fetching from the website. The website I am targeting is "http://trackandtrace.courierpost.co.nz/Search/". I have made the code to embed tracking number after that link and to fetch other fields but the code is not fetching any data it just opens up the link using internet explorer. The error i get is
Here is my code:
Sub Yellowcom()
'Dim ieObj As InternetExplorer
Dim htmlELe As IHTMLElement
Dim HTML As HTMLDocument
Dim i As Integer
Dim x As Integer
Dim URL As String
Dim URLParameter As String
Dim page As Long
Dim links As Object
Dim IE As Object
i = 1
Set IE = CreateObject("InternetExplorer.Application")
'Set ieObj = New InternetExplorer
IE.Visible = True
URL = "http://trackandtrace.courierpost.co.nz/search/"
'Application.Wait Now + TimeValue("00:00:05")
x = 1
For page = 2 To 10
If page > 1 Then URLParameter = Sheet1.Cells(x, 1).Value
IE.navigate URL & URLParameter
' Wait for the browser to load the page
Do Until IE.readyState = 4
DoEvents
Loop
Set HTML = IE.document
Set OrganicLinks = HTML.getElementsByClassName("search-results organic")
Set links = OrganicLinks.Item(0).getElementsByClassName("info")
For Each htmlELe In links
With ActiveSheet
.Range("A" & i).Value = htmlELe.Children(0).textContent
On Error Resume Next
.Range("B" & i).Value = htmlELe.getElementsByClassName("track-visit-website")(0).href
On Error GoTo 0
On Error Resume Next
.Range("C" & i).Value = htmlELe.getElementsByClassName("info-section info-secondary")(0).href
On Error GoTo 0
'.Range("B" & i).Value = htmlELe.getElementsByTagName("a")(0).href
'.Range("C" & i).Value = htmlELe.Children(2).textContent
.Range("D" & i).Value = htmlELe.Children(2).querySelector("a[href]")
'links2 = htmlELe.getElementsByClassName("links")(1)
' .Range("D" & i).Value = links2.href
End With
i = i + 1
x = x + 1
Next htmlELe
Next page
IE.Quit
Set IE = Nothing
End Sub
I highly recommend you use background objects to send info to websites, e.g. the following MSXML2 objects can be used to send GET and POST requests, in the following code I'm sending a request to your website with the search code (pulled from values in column A) and then putting your required delivery status and time xml in column B
Instead of putting the text into column B you can just scrape your data from it. Using this method means you don't have to see anything on the screen, no creating internet explorer instances, no waiting for pages to load etc. it's all handled automatically.
Please read all comments. Let us know where there are problems. Otherwise it will not work I think.