J
Jonathan Sachs
I have a VBA application which loads and reads Web pages through
Internet Explorer. I last used it a couple of months ago with Word
2000 under Windows 2000, and it worked correctly. I am now running on
a Windows XP machine with Word XP, and it doesn't work.
I load Internet Explorer in a class module and use the
DocumentComplete event to make the application wait while a page is
being loaded. Here are the relevant parts of the code:
Private Const pageLoadTimeout As Single = 30 ' 30 second
timeout to load page.
Public WithEvents explorerObject As InternetExplorer
Private explorerWait As Boolean
' The DocumentComplete event routine.
Private Sub explorerObject_DocumentComplete(ByVal pDisp As Object, url
As Variant)
On Error Resume Next
If pDisp Is Nothing Then Exit Sub
If pDisp.Document Is Nothing Then Exit Sub
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Exit Sub
End If
On Error GoTo 0
explorerWait = False
End Sub
' Called after a page is loaded to wait until load is complete.
Private Function waitForDocumentComplete() As Boolean
Dim before As Single, after As Single
before = Timer
Do While explorerWait
after = Timer
If after < before Then before = before - 24 * 60 * 60 ' 'round
Midnight
If after - before > pageLoadTimeout Then
MsgBox "Browser timed out loading page " +
explorerObject.Document.url, vbOKOnly
waitForDocumentComplete = False
Exit Function
End If
DoEvents
If Me.explorerObject.Document.body.all.length > 0 Then
explorerWait = False
Loop
waitForDocumentComplete = True
End Function
' One of several functions that load a page, presented here as an
' example of how waitForDocumentComplete is used.
Public Function htmlGetBody(url As String) As HTMLBody
' Read the specified URL with the specified browser. Return the
' body.
explorerWait = True
explorerObject.navigate url
If waitForDocumentComplete Then
Set htmlGetBody = Me.explorerObject.Document.body
Else
Call cleanup
End
End If
End Function
I haven't figured out exactly what is going wrong, and I think the
problem is manifesting itself in several ways. One is that a reference
to the expression "Me.explorerObject.Document" generates an Automation
Error when a page is not yet loaded. Setting "On Error Resume Next"
doesn't prevent this, and I haven't found any way of detecting the
condition in code. Another manifestation is that the code sometimes
finds that Me.explorerObject.Document.body.all.length has a value
greater than 0 before loading is complete, and if the program
proceeds, any reference to a tag in the body will return Nothing.
I need to know what changes Microsoft made to this interface, and how
to accommodate them!
My mail address is jsachs177 at earthlink dot net.
Internet Explorer. I last used it a couple of months ago with Word
2000 under Windows 2000, and it worked correctly. I am now running on
a Windows XP machine with Word XP, and it doesn't work.
I load Internet Explorer in a class module and use the
DocumentComplete event to make the application wait while a page is
being loaded. Here are the relevant parts of the code:
Private Const pageLoadTimeout As Single = 30 ' 30 second
timeout to load page.
Public WithEvents explorerObject As InternetExplorer
Private explorerWait As Boolean
' The DocumentComplete event routine.
Private Sub explorerObject_DocumentComplete(ByVal pDisp As Object, url
As Variant)
On Error Resume Next
If pDisp Is Nothing Then Exit Sub
If pDisp.Document Is Nothing Then Exit Sub
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Exit Sub
End If
On Error GoTo 0
explorerWait = False
End Sub
' Called after a page is loaded to wait until load is complete.
Private Function waitForDocumentComplete() As Boolean
Dim before As Single, after As Single
before = Timer
Do While explorerWait
after = Timer
If after < before Then before = before - 24 * 60 * 60 ' 'round
Midnight
If after - before > pageLoadTimeout Then
MsgBox "Browser timed out loading page " +
explorerObject.Document.url, vbOKOnly
waitForDocumentComplete = False
Exit Function
End If
DoEvents
If Me.explorerObject.Document.body.all.length > 0 Then
explorerWait = False
Loop
waitForDocumentComplete = True
End Function
' One of several functions that load a page, presented here as an
' example of how waitForDocumentComplete is used.
Public Function htmlGetBody(url As String) As HTMLBody
' Read the specified URL with the specified browser. Return the
' body.
explorerWait = True
explorerObject.navigate url
If waitForDocumentComplete Then
Set htmlGetBody = Me.explorerObject.Document.body
Else
Call cleanup
End
End If
End Function
I haven't figured out exactly what is going wrong, and I think the
problem is manifesting itself in several ways. One is that a reference
to the expression "Me.explorerObject.Document" generates an Automation
Error when a page is not yet loaded. Setting "On Error Resume Next"
doesn't prevent this, and I haven't found any way of detecting the
condition in code. Another manifestation is that the code sometimes
finds that Me.explorerObject.Document.body.all.length has a value
greater than 0 before loading is complete, and if the program
proceeds, any reference to a tag in the body will return Nothing.
I need to know what changes Microsoft made to this interface, and how
to accommodate them!
My mail address is jsachs177 at earthlink dot net.