Hello there,
I have a script that opens IE, inputs some data into a form, then copies and pastes the data, closes IE. The script loops, doing the whole process again.
What I'm finding is it doesn't properly close IE, it does close IE, but iexplore.exe is showing in the Task Manager. When the code runs around 10 times, I have 10 instances of iexplore.exe in the Task Manager! I'm hoping to loop the script hundreds of times!
Is there a way to properly 'kill' IE? IE.Quit is only doing half a job really.
I have a script that opens IE, inputs some data into a form, then copies and pastes the data, closes IE. The script loops, doing the whole process again.
What I'm finding is it doesn't properly close IE, it does close IE, but iexplore.exe is showing in the Task Manager. When the code runs around 10 times, I have 10 instances of iexplore.exe in the Task Manager! I'm hoping to loop the script hundreds of times!
Is there a way to properly 'kill' IE? IE.Quit is only doing half a job really.
Code:
'Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub GetTravelData()
Dim frompostcode As String
Dim topostcode As String
Dim traveldt As Date
Dim traveltime As Date
Dim traveltimestr As String
Dim traveldtstr As String
frompostcode = ThisWorkbook.Sheets("Postcodes").Cells(2, 5).Value
topostcode = ThisWorkbook.Sheets("Postcodes").Cells(2, 6).Value
traveldt = ThisWorkbook.Sheets("Postcodes").Cells(4, 6).Value
traveltime = ThisWorkbook.Sheets("Postcodes").Cells(5, 6).Value
traveltimehr = Format$(Hour(traveltime), "00")
traveltimemin = Format$(Minute(traveltime), "00")
traveltimestr = traveltimehr + ":" + traveltimemin
daystr = Format(traveldt, "ddd")
mthstr = MonthName(Month(traveldt), True)
ddstr = Format$(Day(traveldt), "00")
yyyystr = Format$(Year(traveldt))
traveldtstr = daystr + " " + mthstr + " " + ddstr + " " + yyyystr
Call GetTravelDataInfo(frompostcode, topostcode, traveldtstr, traveltimestr)
End Sub
Sub GetTravelDataInfo(frmpostcode As String, topostcode As String, traveldtstr As String, traveltime As String)
Dim IE As InternetExplorer
Set IE = New InternetExplorer
Dim TravelineHomePage As HTMLDocument
IE.Navigate2 "www.traveline.info"
IE.Visible = True
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Dim inputfield As Object
Set inputfield = IE.document.getElementById("origin")
inputfield.Value = frmpostcode
Set inputfield = IE.document.getElementById("destination")
inputfield.Value = topostcode
Set inputfield = IE.document.getElementById("datePicker")
inputfield.Value = traveldtstr
Set dropOptions = IE.document.getElementsByClassName("timeIntervals")(0)
For Each o In dropOptions.Options
If o.Value = traveltime Then
o.Selected = True
Exit For
End If
Next
Set objcollection = IE.document.getElementsByTagName("button")
i = 0
While i < objcollection.Length
If objcollection(i).Type = "submit" Then
Set objElement = objcollection(i)
End If
i = i + 1
Wend
objElement.Click
startrow = 2
While True
Do While IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
'Sleep 10000
Application.Wait (Now + TimeValue("00:00:06"))
Set journeycollection = IE.document.getElementsByClassName("journey-summary")
'MsgBox journeycollection.Length
For Each o In journeycollection
If (ThisWorkbook.Sheets("Postcodes").Cells(10, 6).Value > ThisWorkbook.Sheets("Postcodes").Cells(12, 6).Value) Then
endfortheday = True
GoTo exitfor
End If
If (o.hasAttribute("tabindex")) Then
Set departtext = o.getElementsByTagName("TD")
ThisWorkbook.Sheets("Postcodes").Cells(startrow, 10).Value = departtext(1).innerText
ThisWorkbook.Sheets("Postcodes").Cells(startrow, 11).Value = departtext(3).innerText
ThisWorkbook.Sheets("Postcodes").Cells(startrow, 12).Value = departtext(4).innerText
ThisWorkbook.Sheets("Postcodes").Cells(startrow, 13).Value = departtext(6).innerText
startrow = startrow + 1
Else
endfortheday = True
GoTo exitfor
End If
Next
exitfor:
If endfortheday Then
GoTo exitwhile
Else
Set laterbutton = IE.document.getElementById("later")
laterbutton.Click
End If
Wend
exitwhile:
IE.Quit
End Sub