Results 1 to 13 of 13
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Internext Explorer automation problem in Word XP (Word XP)

    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 worke. I am now using a Windows XP machine with Word XP, and it misbehaves.

    I load Internet Explorer in a class module. When I load a page I set a variable "explorerWait" to True and then call a function "waitForDocumentComplete" which tests the variable repeatedly until the DocumentComplete event sets it to False.

    When I tried ran the code last night, DocumentComplete appeared to get called as soon as IE started loading the page, letting my code try to refer to the page's body before any data was there. I tried to evade the problem by modifying waitForDocumentComplete to test whether the document was REALLY complete when DocumentComplete said it was. This did not work; a reference to the Internet Explorer object's Document property yielded an Automation Error when the document was not complete, and setting On Error Resume Next did not stop it.

    Returning to the problem after some sleep, I instrumented "waitForDocumentComplete" and "DocumentComplete" to display a trace of the execution path, and I loaded a web page. waitForDocumentComplete returned immediately, before the page finished loading. My trace showed that "DocumentComplete" was never called at all.

    Here is a simplified version of "waitForDocumentComplete":

    <pre>Private Function waitForDocumentComplete() As Boolean
    Debug.Print "On entry to waitForDocumentComplete, _
    explorerWait =", explorerWait
    Do While explorerWait
    Debug.Print " 2. explorerWait =", explorerWait
    DoEvents
    Debug.Print " 5. explorerWait =", explorerWait
    Debug.Print Me.explorerObject.Document.body.all.length, "'" + _
    Me.explorerObject.StatusText + "'", Me.explorerObject.Busy
    If Me.explorerObject.Document.body.all.length > 0 And _
    Me.explorerObject.StatusText = "Done" And _
    Not Me.explorerObject.Busy Then
    explorerWait = False
    End If
    Debug.Print " 6. explorerWait =", explorerWait
    Loop
    waitForDocumentComplete = True
    End Function
    </pre>


    I see no way this code could possibly display "5. explorerWait = True" and then "6.explorerWait = False" without displaying a line with three property values in between. But that is exactly what happened -- reproducibly. I closed and restarted Word in case the Automation Error had messed up the interpreter's state; it made no difference.

    I then commented out the statement "Debug.Print Me.explorerObject..." and the following If... EndIf block. This returned the code to its original state except for some added trace statements. Then it worked.

    Although my code appears to be working now, I don't know what broke it or what made it start working again, so I don't know when it will break again, or what I can do when it does.

    Has anyone encountered a similar problem? Any ideas what to do?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Internext Explorer automation problem in Word XP (Word XP)

    I have no way of testing your code. The only thing that strikes me is that you use + to concatenate strings. In VBA, you can concatenate strings with & (the "standard" way) and with +. They behave differently inf one of the components is Null: if varA is "Word" and varB is Null, then varA & varB will evaluate to "Word", but varA + varB will evaluate to Null.

    By the way, if you comment out the If ... End If block, how is explorerWait ever going to be False? I would expect the Do While loop to run forever.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Internext Explorer automation problem in Word XP (Word XP)

    I use this approach:

    <pre>'Declare Sleep API {in Module Declarations area}
    Private Declare Sub Sleep Lib "kernel32" (ByVal nMilliseconds As Long)

    Sub Whatever()
    ...
    'Create browser object references
    ' Requires a reference to Microsoft Internet Controls (shdocvw.dll)
    Dim ieSrc As New InternetExplorer

    'Load intranet page
    With ieSrc
    .Visible = True 'show window and load page
    .navigate "http://server/folder/pg.asp?param=" & strValue
    While Not .readyState = READYSTATE_COMPLETE
    Sleep 500 'wait 1/2 sec before trying again
    Wend
    End With

    'Create document object model references
    ' Requires a reference to Microsoft HTML Object Library (mshtml.tlb)
    Dim ieDocSrc As MSHTML.HTMLDocument
    Set ieDocSrc = ieSrc.Document
    ...</pre>

    I don't know if you could use this in a class module; I have not explored them much.

  4. #4
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Internext Explorer automation problem in Word XP (Word XP)

    HansV wrote, "The only thing that strikes me is that you use + to concatenate strings."

    That could, of course, affect the trace output but not the original problem. It's an interesting fact; I'm sure I encountered the '&' operator when I began working with VBA, but I had forgotten about it completely.

    As an experiment, I tried entering the immediate statement:

    <pre> ? "xyz", "'"+null+"'", "xyz"
    </pre>

    The interpreter displayed:

    <pre> xyz Null xyz
    </pre>

    That evidently was not a factor.

    > By the way, if you comment out the If ... End If block, how is explorerWait ever going to be False?

    It will be false when the DocumentComplete event routine sets it to false. I believe I explained that in the original post, but I did not show it in the example.

  5. #5
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Internext Explorer automation problem in Word XP (Word XP)

    That's a solution to the problem which I never encountered before. I don't know if it will work better, but since I still can't figure out what went wrong, I can't say that it won't! I will certainly try it if, or when, the problem returns. The polling approach is less efficient than the event-driven approach, but in this application there is no practical difference, and if it works...

    By the way, how did you find out about .readyState?

    One of my ongoing frustrations with the IE object model is that there is no coherent documentation for it; when I need an unfamiliar feature, I have to look for a method or property that is "documented" by its name alone in the auto-complete pop ups, or search for a reference with Google. This one is right there in the pop up, but I never noticed it before.

  6. #6
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Internext Explorer automation problem in Word XP (Word XP)

    >>>>By the way, how did you find out about .readyState?

    do a search on your computer for the file "HTMLREF.CHM" - in this help file you should get some useful info - the section you're probably after is "reference for visual basic developers" which lurks under the path Internet tools and technologies/reusing the webrowser and mshtml / reusing the webbrowser control

    note however, that this is for IE 5.0, not 6. i don't know how much difference there is between the two.

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Internext Explorer automation problem in Word XP (Word XP)

    I probably found it in a sample article on MSDN. It was a loooong time ago...

  8. #8
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Internext Explorer automation problem in Word XP (Word XP)

    Well, I tried it, and it didn't work. (This took a week because I can only test the code when I need to use it: when a batch of my auctions ends.)

    I modified the code to loop until the DocumentComplete event occurs AND .ReadyState = READYSTATE_COMPLETE. At the end of the loop, references to .Document.Body.All.Length yielded a "property does not exist" error.

    A five-second sleep at the end of the loop made the code work correctly. That's the solution I am using for now, but it's viable only as a desperation measure.

    When I find time I'm going to extract the misbehaving parts of the code to see if I can solve the problem in isolation. If I don't make progress, would anyone else be willing to take a look? The whole test case will probably be 50 to 100 lines of code.

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Internext Explorer automation problem in Word XP (Word XP)

    I could look at it at some point in the next two weeks. Does the page you're loading involve frames/iframes or ActiveX controls? Just wondering how it could not be "ready" when it is supposed to be?

  10. #10
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Internext Explorer automation problem in Word XP (Word XP)

    Thanks; when I have the chance to do the work I'll report my findings, and post the code if appropriate. As I explained at the start of the thread, the problem has nothing to do with loading a particular page; it has to do with migration from Windows 2000 and Word 2000 to Windows XP and/or Word 2002.

    You do have Windows XP and Word 2002, I hope? If not, you probably will not be able to reproduce the problem (and if you can, I won't know what to make of your results).

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Internext Explorer automation problem in Word XP (Word XP)

    I do have XP and XP. I don't use that particular project any more because IE6 broke something in it, and I ended up switching to a completely different approach.

  12. #12
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Internext Explorer automation problem in Word XP (Word XP)

    You haven't heard from me because when I tried to isolate the failing code in a small module I could send to you, I couldn't get it to fail. I fell back to my original template and couldn't get that to fail either.

    And this was after almost three weeks of repeated efforts, consuming hours, in which I couldn't get it to work!

    I never figured out why it was failing. Now I don't know why it's working, either!

    If it ever starts to misbehave again, I will be in touch.

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Internext Explorer automation problem in Word XP (Word XP)

    Was it during that time that Mars was really close to the Earth? Maybe that was it. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •