Log in

View Full Version : Help returning text from webpage to cell



sdrloveshim
05-13-2016, 07:26 AM
I'm looking at FedEx for tracking number

671300298550



I'm attempting to return the the delivered date Sat 4/30/2016 4:08 pm to a cell in Excel. Right now it's just writing Error into the cell.

Keep in mind I'm very new to VBA. I may be doing something that looks obviously wrong. Please explain you answers in the simple terms if possible. Any help is appreciated

Here is the code I'm running.

Public Sub FedExTracking()

Dim ie As Object
Dim ProURL As String
Dim RowCount As Integer
Dim iCounter As Integer
Dim links As Variant, lnk As Variant

Set ie = CreateObject("InternetExplorer.application")
RowCount = 0

Do While Not ActiveCell.Offset(RowCount, -1).Value = ""
'apparently I'm not allowed to post links but if you just go to the fedex website and run the tracking number listed you can see what I see.
ProURL = "beginning of link" & ActiveCell.Offset(RowCount, -1).Value & "end of link"

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 links = ie.document.getElementsByTagName("div")

For Each lnk In links
If lnk.className = "snapshotController_date.dest" Then
ActiveCell.Offset(RowCount, 0).Value = lnk.innerText
Else
ActiveCell.Offset(RowCount, 0).Value = "Error"
End If
Exit For
Next

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

rstuck
05-13-2016, 02:48 PM
OK, we should check a couple of things. Set a break point on the For Each lnk in Links. Run the code. Run to the next line (F8). Check what the lnk.classname value. is it snapshotcontroller_date.dest ? any extra spaces will make sure that it will not match. Check what the lnk.innerText value is.
you can also put a DEBUG.PRINT lnk.ClassName and bring up the immediate window (ctl +G is a shortcut ). this will print out the values so you can see what the value is.