View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default Mileage Calculator by post code / zip code

Hi,

I can't get a reference to the table either but can cheat. This will
populate the 4 cells below the postcode with the 4 distances the web page
calculates. I hope someone comes up with a less messy solution for you

Private Sub CommandButton1_Click()
For Each c In Worksheets("Sheet1").Range("D6:N6").Cells
If c.Offset(0, 1).Value < "" Then
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "http://www.driving-distances.com/distances-between-calculator.php"
IE.Navigate2 URL
Do While IE.readyState < 4
DoEvents
Loop
Do While IE.busy = True
DoEvents
Loop
Set Form = IE.document.getElementsByTagname("Form")
Set inputform = Form.Item(0)

Set Postcodebox = inputform.Item(0)
Postcodebox.Value = c.Value

Set Postcodebox2 = inputform.Item(1)
Postcodebox2.Value = c.Offset(0, 1).Value

Set POSTCODEbutton = inputform.Item(2)
POSTCODEbutton.Click

Do While IE.busy = True
Loop
RowCount = 1
For Each itm In IE.document.all
If Val(itm.innertext) < 0 Then
c.Offset(RowCount, 1) = itm.innertext
RowCount = RowCount + 1
End If
Next itm
IE.Quit
End If
Next
End Sub

Mike

"Atishoo" wrote:

Hi
I am adapting the following sub to work with a more accurate web site (one
that calculates distances based on the full post code)
I am getting object variable / block variable not set on the last line:
c.Offset(1, 1) = Val(Trim(DistanceRow.Cells(2).innertext)).
Am i referencing the IE table incorectly?
Any ideas apreciated.

Private Sub CommandButton1_Click()

For Each c In Worksheets("Sheet1").Range("D6:N6").Cells
If c.Offset(0, 1).Value < "" Then

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

URL = "http://www.driving-distances.com/distances-between-calculator.php"
IE.Navigate2 URL
Do While IE.readyState < 4
DoEvents
Loop
Do While IE.busy = True
DoEvents
Loop


Set Form = IE.document.getElementsByTagname("Form")
Set inputform = Form.Item(0)

Set Postcodebox = inputform.Item(0)
Postcodebox.Value = c.Value

Set Postcodebox2 = inputform.Item(1)
Postcodebox2.Value = c.Offset(0, 1).Value

Set POSTCODEbutton = inputform.Item(2)
POSTCODEbutton.Click

Do While IE.busy = True
Loop

Set Table = IE.document.getElementsByTagname("table")
Set DistanceTable = Table.Item(1)

Set DistanceRow = DistanceTable.Rows(6)

c.Offset(1, 1) = Val(Trim(DistanceRow.Cells(2).innertext))

IE.Quit
End If
Next

End Sub