IE VBA - Wait for word to appear

stilesbn

Board Regular
Joined
Feb 21, 2011
Messages
66
I'm currently using a macro to enter information on an internet page and click through a number of forms. I'm running into a problem that my do until ready state loop isn't waiting for the page to completely load before the macro continues on. Here is my wait code:

Code:
                Dim objCollection As Object
                Dim objElement As Object
                
                Set objCollection = .document.getElementsByTagName("input")
                
                i = 0
                While i < objCollection.Length
                    If objCollection(i).Type = "submit" And _
                        objCollection(i).Name = "ctl00$MainContent$ImportWizard$wzImportRecords$StartNavigationTemplateContainerID$StartNextButton" Then
                            Set objElement = objCollection(i)
                    End If
                    i = i + 1
                Wend
                objElement.Click
                
                Do Until .readyState = 4
                    DoEvents
                Loop

Is there a way to write a loop that searches the page for an actual text to show up and then end the loop.

So from a sample of the HTML code, I would be waiting for the words "Data Preview:" to appear as it is unique to the page that is loading.

HTML:
<td align="left" style="color:White;background-color:#5D7B9D;border-style:Solid;font-weight:bold;">Batch: 4113      File: </td>
	</tr><tr style="height:100%;">
		<td valign="top" style="color:#5D7B9D;border-width:0px;">
            <table width="100%">
                <tr>
                    <td align="Left">
                        Data Preview:
                    </td>
                </tr>
                <tr>
                    <td>
                        <div style="width: 800px; overflow: scroll;">
                            <div>
			<table cellspacing="0" rules="all" border="1" id="ctl00_MainContent_ImportWizard_wzImportRecords_gvPreviewData" style="border-collapse:collapse;">

Being able to wait for a word to appear would help in some other situations I have too.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I have been wondering this as well. I have webpages that load, and are in a "readystate" but the images on them have not. To get around this, I delay my macro a bit.

The two methods I use as a work around are:

Delay macro for .5 seconds

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
sleep 500

Delay Macro for 1 second

Application.Wait Now + TimeValue("00:00:01")
 
Upvote 0
I do have a wait function, though I was hoping to get around that as wait times can vary from 1 second to 10 seconds depending on the file size I'm loading. Currently I plan on getting around it by varying the wait times depending on files size, but a way to wait for a word would open up some new worlds with endless possibilities! :)
 
Upvote 0
There are other loops you can try depending on what you are actually waiting on.

In your current code I'm not 100% sure what you are waiting for, is it IE or the document?

Here are a couple of examples of the loops I mean.
Code:
Do While IE.Busy: DoEvents: Loop
 
Set doc = IE.document
 
Do While doc.ReadyState <> "complete": DoEvents: Loop

You could also just loop through the code you are using until objElement isn't Nothing
Code:
Do 
 
i = 0
                While i < objCollection.Length
                    If objCollection(i).Type = "submit" And _
                        objCollection(i).Name = "ctl00$MainContent$ImportWizard$wzImportRecords$StartNavigationTemplateContainerID$StartNextButton" Then
                            Set objElement = objCollection(i)
                    End If
                    i = i + 1
                Wend
 
Loop Until Not objElement Is Nothing

If the button is for submitting a form there might be other ways to deal with it.

For example, use the form's submit method.

All of the above kind of depends on what type of page it is you are loading, I'm guessing it's an ASP page.
 
Upvote 0
I never knew there was a difference between IE waiting and the document waiting. I have set the code to wait for both IE and the document (just to make sure the waiting gets done) and it is working wonderfully for my purposes now. Thanks!
 
Upvote 0
There are other loops you can try depending on what you are actually waiting on.

In your current code I'm not 100% sure what you are waiting for, is it IE or the document?

Here are a couple of examples of the loops I mean.
Code:
Do While IE.Busy: DoEvents: Loop
 
Set doc = IE.document
 
Do While doc.ReadyState <> "complete": DoEvents: Loop

You could also just loop through the code you are using until objElement isn't Nothing
Code:
Do 
 
i = 0
                While i < objCollection.Length
                    If objCollection(i).Type = "submit" And _
                        objCollection(i).Name = "ctl00$MainContent$ImportWizard$wzImportRecords$StartNavigationTemplateContainerID$StartNextButton" Then
                            Set objElement = objCollection(i)
                    End If
                    i = i + 1
                Wend
 
Loop Until Not objElement Is Nothing

If the button is for submitting a form there might be other ways to deal with it.

For example, use the form's submit method.

All of the above kind of depends on what type of page it is you are loading, I'm guessing it's an ASP page.
Norie,
would the above options work if the IE webpage loads a pd image from a database? The ie webpage is in a "readystate" but the image may not load for another 2-3 seconds, if at all. Right now I have a 4 second delay on my actions post load.
 
Upvote 0
It's hard to tell without more information.

What exactly do you do on the page that leads to this image being retrieved?

eg do you select something from a list, hit a button?

It sounds to me that what's happening is IE is in a ready state but the document isn't complete.

You might want to stick in the loop to wait for the document to complete.

Actually when I'm coding this sort of thing I use both the code to wait for IE and the code to wait for the document.

Pretty sure it's not always needed but it usually does not harm, perhaps slows the code a bit but nothing really noticeable, so far anyway.
 
Upvote 0
It's hard to tell without more information.

What exactly do you do on the page that leads to this image being retrieved?

eg do you select something from a list, hit a button?

It sounds to me that what's happening is IE is in a ready state but the document isn't complete.

You might want to stick in the loop to wait for the document to complete.

Actually when I'm coding this sort of thing I use both the code to wait for IE and the code to wait for the document.

Pretty sure it's not always needed but it usually does not harm, perhaps slows the code a bit but nothing really noticeable, so far anyway.

Hi Norie,
I am going to try your suggestion.
The website is html.
I navigate through it using the source code id for the button, as such....

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "website"
IE.Visible = True
While IE.Busy
DoEvents 'wait until IE is done loading page.
Wend
IE.Document.all("Login_txtUsername").Value = Range("B3").Value

After login, and invoice number and date are entered by the macro, and the macro clicks the "view invoice" button.

This loads a PDF image, except it is in an IE window.
I know it is a PDF since it saves as on, and the only code that can get the window to close is the AcroPDF object.


I would pretty much like to use the below code, but without the "wait", as wait times can vary.

Dim Shell As Object
Dim AcroPDF As Object
Set Shell = CreateObject("Shell.Application")
For Each AcroPDF In Shell.Windows
If TypeName(AcroPDF.Document) = "AcroPDF" Then
AcroPDF.Visible = True
AcroPDF.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
Application.Wait Now + TimeValue("00:00:02")
Application.SendKeys "~", True
'Application.Wait Now + TimeValue("00:00:02")
AcroPDF.Quit
End If
 
Upvote 0
Can you post the URL for the site?

Also how have you determined it's an HTML page?
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top