If I run this in the editor sometimes it works and sometimes it does not.
If I run this in editor with F8, stepping through the code, it does work. I can only guess that enough time is passing during the F8 presses that the page can load.
But is that not the purpose of adding the wait code?
Private Const READYSTATE_COMPLETE = 4
Private Const TimeOutSeconds As Double = 15
Sub IeP()
Dim objIE As Object
Dim TimeOutTime As Date
'create the IE object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Top = 0
objIE.Left = 0
objIE.Width = 1000
objIE.Height = 800
objIE.Visible = True
'objIE.AddressBar = 0
'objIE.StatusBar = 0
'objIE.Toolbar = 0
'navigate to the site
objIE.Navigate ("http://thewebsite.com")
'add 15 seconds to the current time and assign
'this value to the variable "TimeOutTime"
TimeOutTime = DateAdd("s", TimeOutSeconds, Now)
Do Until objIE.ReadyState = READYSTATE_COMPLETE
DoEvents
If Now > TimeOutTime Then
'timed out
MsgBox "Timed out..."
End If
Loop
'I WAS USING BELOW BUT SWITCHED TO ABOVE
'Do
'DoEvents
' If Err.Number <> 0 Then
' objIE.Quit
' Set objIE = Nothing
' GoTo the_start
' End If
'Loop Until objIE.ReadyState = 4
Set the_input_elements = objIE.Document.getElementsByTagName("input")
For Each input_element In the_input_elements
If input_element.getAttribute("name") = "username" Then
input_element.Value = "PRIVATE"
'Exit For
End If
If input_element.getAttribute("name") = "password" Then
input_element.Value = "NNNNNNN"
Exit For
End If
Next input_element
Set the_input_elements = objIE.Document.getElementsByTagName("input")
For Each input_element In the_input_elements
If input_element.getAttribute("value") = "Login" Then
input_element.Click
Exit For
End If
Next input_element
'add 15 seconds
TimeOutTime = DateAdd("s", TimeOutSeconds, Now)
Do Until objIE.ReadyState = READYSTATE_COMPLETE
DoEvents
If Now > TimeOutTime Then
'timed out
MsgBox "Timed out..."
End If
Loop
Set the_input_elements = objIE.Document.getElementsByTagName("input")
For Each input_element In the_input_elements
If input_element.getAttribute("value") = "Launch Page" Then
input_element.Click
Exit For
End If
Next input_element
'add 15 seconds
TimeOutTime = DateAdd("s", TimeOutSeconds, Now)
Do Until objIE.ReadyState = READYSTATE_COMPLETE
DoEvents
If Now > TimeOutTime Then
'timed out
MsgBox "Timed out..."
End If
Loop
'add 15 seconds
TimeOutTime = DateAdd("s", TimeOutSeconds, Now)
Do Until objIE.ReadyState = READYSTATE_COMPLETE
DoEvents
If Now > TimeOutTime Then
'timed out
MsgBox "Timed out..."
End If
Loop
'open the next page
Set the_input_elements = objIE.Document.getElementsByTagName("input")
For Each input_element In the_input_elements
If input_element.getAttribute("value") = "thenextpage" Then
input_element.Click
Exit For
End If
Next input_element
'add 15 seconds
TimeOutTime = DateAdd("s", TimeOutSeconds, Now)
Do Until objIE.ReadyState = READYSTATE_COMPLETE
DoEvents
If Now > TimeOutTime Then
'timed out
MsgBox "Timed out..."
End If
Loop
'objIE.Quit
'Set objIE = Nothing
End Sub
Bookmarks