Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliseconds As Long)
Private Sub CommandButton1_Click()
Dim objExplorer As Object
Set objExplorer = CreateObject("InternetExplorer.Application")
With Range("Adressen")
caintRow = .Rows.Count
strnaarplaats = Trim(.Cells(1, 1))
strnaarpostcode = Trim(.Cells(1, 2))
strnaarland = Trim(.Cells(1, 3))
For iaintRow = 2 To caintRow
strvanplaats = strnaarplaats
strvanpostcode = strnaarpostcode
strvanland = strnaarland
If .Cells(iaintRow, 1) <> vbNullString Then
strnaarplaats = Trim(.Cells(iaintRow, 1))
strnaarpostcode = Trim(.Cells(iaintRow, 2))
strnaarland = Trim(.Cells(iaintRow, 3))
strLink = "http://maps.google.nl/maps?f=d&source=s_d&saddr=" & strvanplaats & "+" & strvanpostcode & "+" & strvanland & "&daddr=" & strnaarplaats & "+" & strnaarpostcode & "+" & strnaarland & "&hl=nl&geocode=&mra=ls&sll=51.238135,5.69546&sspn=0.121661,0.356712&ie=UTF8&z=12"
objExplorer.navigate (strLink)
objExplorer.Visible = True
Do While objExplorer.Busy Or objExplorer.readystate <> 4 'aanpassing 3
DoEvents
Sleep 500 'aanpassing 3
Loop
strSearchedIn = objExplorer.document.body.innerhtml
intPosition1 = InStr(1, strSearchedIn, "Bedoelde u:")
If intPosition1 <> 0 Then
Range("Traject").Cells(iaintRow, 1) = "Adresfout"
Else
strSearchedFor1 = "Routebeschrijving naar"
intPosition1 = InStr(1, strSearchedIn, strSearchedFor1)
strSearchedFor2 = "<DIV><B>"
intPosition2 = InStr(intPosition1, strSearchedIn, strSearchedFor2) + Len(strSearchedFor2)
strSearchedFor3 = "</B>"
intPosition3 = InStr(intPosition2, strSearchedIn, strSearchedFor3)
strAfstand = Replace(Mid(strSearchedIn, intPosition2, intPosition3 - intPosition2), " km", "")
dblAfstand = CDbl(strAfstand)
Range("Traject").Cells(iaintRow, 1) = dblAfstand
End If
End If
Next iaintRow
End With
With Range("Adressen")
caintRow = .Rows.Count
strvanplaats = Trim(.Cells(1, 1))
strvanpostcode = Trim(.Cells(1, 2))
strvanland = Trim(.Cells(1, 3))
For iaintRow = 2 To caintRow
If .Cells(iaintRow, 1) <> vbNullString Then
strnaarplaats = Trim(.Cells(iaintRow, 1))
strnaarpostcode = Trim(.Cells(iaintRow, 2))
strnaarland = Trim(.Cells(iaintRow, 3))
strLink = "http://maps.google.nl/maps?f=d&source=s_d&saddr=" & strvanplaats & "+" & strvanpostcode & "+" & strvanland & "&daddr=" & strnaarplaats & "+" & strnaarpostcode & "+" & strnaarland & "&hl=nl&geocode=&mra=ls&sll=51.238135,5.69546&sspn=0.121661,0.356712&ie=UTF8&z=12"
objExplorer.navigate (strLink)
objExplorer.Visible = True
Do While objExplorer.Busy Or objExplorer.readystate <> 4 'aanpassing 3
DoEvents
Sleep 500 'aanpassing 3
Loop
strSearchedIn = objExplorer.document.body.innerhtml
intPosition1 = InStr(1, strSearchedIn, "Bedoelde u:")
If intPosition1 <> 0 Then
Range("Traject").Cells(iaintRow, 1) = "Adresfout"
Range("Theoretische_afstand").Cells(iaintRow, 1) = "Adresfout"
Else
strSearchedFor1 = "Routebeschrijving naar"
intPosition1 = InStr(1, strSearchedIn, strSearchedFor1)
strSearchedFor2 = "<DIV><B>"
intPosition2 = InStr(intPosition1, strSearchedIn, strSearchedFor2) + Len(strSearchedFor2)
strSearchedFor3 = "</B>"
intPosition3 = InStr(intPosition2, strSearchedIn, strSearchedFor3)
strAfstand = Replace(Mid(strSearchedIn, intPosition2, intPosition3 - intPosition2), " km", "")
dblAfstand = CDbl(strAfstand)
Range("Theoretische_afstand").Cells(iaintRow, 1) = dblAfstand
End If
End If
Next iaintRow
End With
objExplorer.Quit
Set objExplorer = Nothing
End Sub