sdrloveshim sdrloveshim - 4 months ago 18
HTML Question

How would I pull this text

I'm looking at this webpage -> https://www.fedex.com/apps/fedextrack/?action=track&tracknumbers=671299425542&locale=en_US&cntry_code=us

And I want to return the delivered date Thu 3/31/2016 12:16 pm

Here is my code so far

Public Sub FedExTracking()

Dim IE As Object
Dim ReturnValue As Object
Dim ProUrl As String
Dim RowCount As Integer
Dim PullText As String
Dim iCounter As Integer

Set IE = CreateObject("InternetExplorer.application")

RowCount = 0

Do While Not ActiveCell.Offset(RowCount, -1).Value = ""

ProUrl = "https://www.fedex.com/apps/fedextrack/?action=track&tracknumbers=" & ActiveCell.Offset(RowCount, -1).Value & "&locale=en_US&cntry_code=us"

With IE
.Visible = True
.Navigate ProUrl
Do Until Not IE.Busy And IE.readyState = 4: DoEvents: Loop
End With


iCounter = 0
Do While iCounter < 8
WaitHalfSec
iCounter = iCounter + 1
Loop


set ReturnValue = IE.document.getElementsClassName("snapshotController_date.dest")(0)

'THIS LINE RETURNS RUN TIME ERROR "91" OBJECT VARIABLE OR WITH BLOCK VARIABLE NOT SET
PullText = ReturnValue.innertext

ActiveCell.Offset(RowCount).Value = PullText & "."

RowCount = RowCount + 1

Loop

IE.Quit
Set IE = Nothing

End Sub

Sub WaitHalfSec()
Dim t As Single
t = Timer + 1 / 2
Do Until t < Timer: DoEvents: Loop
End Sub


I was able to get locate and store the line it seems as long as I'm not tyring to get to the innertext. How would I go about returning date on this line?

< div class = "snapshotController_date.dest" > Thu 3/31/2016 12:16 pm < / div >

Any help is appreciated!

Answer

I was able to get this working. Just putting the answer out there for anyone that needs it.

Public Sub FedExTrackingWorking()
Dim ie As Object
Dim ProURL As String
Dim iCounter As Integer
Dim htmlColl As MSHTML.IHTMLElementCollection
Dim htmlInput As MSHTML.HTMLInputElement
Dim RowCount As Integer

RowCount = 0

Set ie = CreateObject("InternetExplorer.application")

Do While Not ActiveCell.Offset(RowCount, -1).Value = ""

ProURL = "https://www.fedex.com/apps/fedextrack/?action=track&tracknumbers=" & ActiveCell.Offset(RowCount, -1).Value & "&locale=en_US&cntry_code=us"

With ie
    .Visible = True
    .navigate ProURL
    Do Until Not ie.Busy And ie.readyState = 4: DoEvents: Loop
End With

iCounter = 0
Do While iCounter < 8
    WaitHalfSec
    iCounter = iCounter + 1
Loop

Set htmlColl = ie.document.getElementsByTagName("div")

For Each htmlInput In htmlColl
    If htmlInput.className = "snapshotController_date dest" Then
        ActiveCell.Offset(RowCount).Value = htmlInput.innerText
        Exit For
    End If
Next htmlInput

RowCount = RowCount + 1

Loop

ie.Quit
Set ie = Nothing

End Sub

Sub WaitHalfSec()
    Dim t As Single
    t = Timer + 1 / 2
        Do Until t < Timer: DoEvents: Loop
End Sub
Comments