Link to home
Start Free TrialLog in
Avatar of TheRealBeale
TheRealBeale

asked on

Internet Explorer ActiveX Control - .busy property doesn't work in do loop.

This problem won't go away.

I'm using a Microsoft Web Browser control embedded into a Excel VBA Userform. I'm writing a script which will automate form entry on an intranet site and proceed through each page.

The lines of code I keep finding when looking for ways to 'wait' for the page to load are as follows.

Do Until webbrowser1.ReadyState = READYSTATE_COMPLETE
 DoEvents
Loop

and

Do until webbrowser1.busy = false
 DoEvents
Loop

Neither of these work. With the first example, the script hangs. It seems that the do loop, despite the do events, doesn't give the control enough 'breathing space' to actually load the page. In the second example, .busy seems to return false even while nested frames are still loading. The next line in the program then fires, (referencing a form variable which isn't there yet) causing it to drop an error message. Even once I've figured this out I need to get some detection for timeouts.

Has anyone else had any problems, specifically when using Excel VBA or does anyone know what I might try next?

Many thanks,

Beale
Avatar of bobbit31
bobbit31
Flag of United States of America image

Do until webbrowser1.busy = true
 DoEvents
Loop
Avatar of TheRealBeale
TheRealBeale

ASKER

That doesn't solve my problem really. I'm trying to wait until .busy = false. We assume it's true to begin with and that it will eventually become false. As I said before the whole

Do until webbrowser1.busy = false
DoEvents
Loop

concept doesn't seem to work...

Or am i missing something..

B
correct, it is true while you are loading the page, so when it is done loading it exits the loop and .busy = false.

i don't see what the problem is?

if you are trying to wait until .busy = false, then it will be after the loop.

maybe i'm missing something.
Tried your code. Still no joy.

Translated into English, my line of code just means 'until the browser stops being busy, do stuff.' Then it should move on.

I just get a 'Object doesn't support this property or method' message at the next line of code which is

UserForm1.WebBrowser1.Document.frames.topFrame.form1.Search.Value = "fhm"

which errors out becuase it can't reference the object because the page hasn't loaded yet. This line works fine once the page has loaded. My problem is getting it to load the page and all nested frames and only then move onto the next line.
so this doesn't work?

Do while webbrowser1.busy = true
   DoEvents
Loop

'' the page is loaded now so this should work
UserForm1.WebBrowser1.Document.frames.topFrame.form1.Search.Value = "fhm"

Can you trap the error and return to the line  

UserForm1.WebBrowser1.Document.frames.topFrame.form1.Search.Value = "fhm"

when the error code is "object doesn't...etc"

In effect, this gives you a "loop" that will loop until the form is loaded.  You could put a counter in there so that it cleanly exits after what you consider to be a reasonable period of time.  Increment it by 1 each time you hit the error and when it gets to 100 or 1000 or whatever, stop trying.
 
 
Avatar of Richie_Simonetti
If i understood: you cannot access the Document object until the page is fully downloadaded, so, if you need to access it use DocumentComplete event of webbrowser1

if (pDisp is webbrowser1.object) then
    WebBrowser1.Document.frames.topFrame.form1.Search.Value = "fhm"

end if
One more thing:
If you need to wait for some reason, don't trust in .Busy property.
Try to set a boolean global variable:
dim bolComplete as boolean

In DocumentComplete event of webbrowser1

if (pDisp is webbrowser1.object) then
   bolComplete=true
end if

and in your loop
do
   'doevents
loop until bolComplete = True
Busy property has always been "flaky" in its functionality.  I usually try and keep it limited to utilizing the DocumentComplete event as stated, or, more reliable than Busy is:


Browser.Navigate "..."
DoEvents

Do Until Browser.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop
Thanks for your help on this, but none of these have worked. Even Microsoft's own documentation states these as possible ways of doing it but still none of them work.

I'm using a standard webbrowser object in Excel 97 VBA, if that's any help. It looks like the whole browser object would be happier if it had it's own thread, but not possible without VBA 6. Seems that the DoEvents doesn't allow the browser any room to process the query. Has anyone else had these kinds of problems with VBA or have any of you got some Excel VBA code known to work.

Many thanks..

Beale
As i alreday told you, don't trust in .busy property of the control.
If you need to wait until page is fully downloaded (busy=False), use DocumentComplete event, believe me, it works.!

You could open your own internet explorer instance if you want.
Set a reference to microsoft internet controls

in general declarations sections of your form:
Dim WithEvents IE As SHDocVw.InternetExplorer

you could use its properties and methods like you did with the webbrowser control.

That doesn't means that you will solve .busy matter with this.
I've stopped using the .busy property as you recommended. My new code uses the DocumentComplete handler.

To test this handler, I told it to fill a label with the word 'Done' when it had finished navigating to a page. So THAT bit works.

Set up a global boolean variable called bolPageLoaded which is set to false to begin with and then is set to true by this handler.

Then there's a

Do Until PageLoaded = True
    DoEvents
Loop

in the main code after the .navigate() method has been called. It's at this point that the problems start, as the page does not submit the form and move on and just hangs there. If you do it manually it's fine.

Would calling an IE instance be more reliable or would I end up in the same pickle?

Cheers

Beale
same pickle.

But maybe you are doing something wrong. How do you populate the form and submit it? Maybe there is the problem.
Regarding populate the form and submit take a look at ..
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TheRealBeale:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
Recommendation: Points to Richie_Simonetti
Richie - looks like you'll get the points. Project has been delayed a bit and I'm going to try out your thinking tonight and then post something back.

Believe I may have tried using this technique before to no avail.

Regards
Beale
Moderator, my recommended disposition is:

    Accept Richie_Simonetti's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer