Sleep in Word/VBA (not Excel Wait)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Sleep in Word/VBA (not Excel Wait)

Post by ChrisGreaves »

Code: Select all

Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("Heading 1")
Selection.TypeText (strcURLHeader & strURL & vbCrLf)
Selection.Style = ActiveDocument.Styles("Normal")
Selection.TypeText (strcSearchStringHeader & strSearchTerm & vbCrLf)
DoEvents ' TWO DOEVENTS ATTEMPT TO OVERCOME TIMING PROBLEMS WITH SOME SEARCH ENGINES.
Selection.TypeText (bot.ActiveElement.Text)
DoEvents ' TWO DOEVENTS ATTEMPT TO OVERCOME TIMING PROBLEMS WITH SOME SEARCH ENGINES.
doc.SaveAs (strFilename)
I experience timing problems from time to time when accessing three of the eight search engines I am driving. Using Selenium drivers. Right now the Chrome Browser driver
My Word2003/VBA code sends a search request to eight search engines, one at a time, through the Chrome Browser, and attempts to capture the search results (text) in a Word document. "Selection.TypeText (bot.ActiveElement.Text)"

Three of the search engines occasionally do not respond in time, and instead of storing the web page (lots of hits!) in the document I get stored just the preamble. So I know that I am executing my code - the URL and search string are dumped, buit not the web page contents (bot.ActiveElement.Text).
If I set a break point to introduce a manual delay, the text is dumped correctly..

The two "DoEvents" are not [always] successful in introducing a delay. And once I get the bit between my teeth and turns this loose, I will not see or know that text is missing!
I think that there is nothing special about the Chrome Driver ("bot"). From Word/VBA's point of view the world out there is a black box and I am waiting for something to come in from the black box.

Excel has WAIT, but Wordies have to depend on SLEEP. (Microsoft Regular Expressions v 1.0 in my case)

If/When I introduce a SLEEP of one second in my VBA code, exactly what goes to sleep?
Word/VBA code for sure; my procedure gets nothing done until after that one-second is up.
Windows can't go to sleep, not totally, or else it would never wake up!

(1) Is the Chrome Driver (bot) going to sleep now? If so that won't do much to help me over the timing problems.
(2) The general question is "How much of the black-box world out there is going to sleep? And what is NOT going to sleep?


Thanks
Chris
An expensive day out: Wallet and Grimace

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: Sleep in Word/VBA (not Excel Wait)

Post by SpeakEasy »

>exactly what goes to sleep?

The thread that calls the Sleep API. VBA runs as a single-thread, so it is VBA that goes to sleep. However, any threads it spawns remain unaffected (and your ActiveX Selenium library will be running in it's own threads)

>The two "DoEvents" are not [always] successful in introducing a delay.
Well, that's not really the purpose of DoEvents. The purposes is to immediately defer to the OS to allow it to process any messages in your apps message queue. This obviously takes a finite (but unknown) amount of time, giving the appearance of a delay., but it isn't something you have any control over

That being said, I am sure that Selenium has a built-in wait capability.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Sleep in Word/VBA (not Excel Wait)

Post by ChrisGreaves »

SpeakEasy wrote:
20 Oct 2021, 10:21
The thread that calls the Sleep API. VBA runs as a single-thread, so it is VBA that goes to sleep.
Thank You speakeasy.
From my VBA programmer point of view then, when VBA issues a sleep, it is VBA that goes to sleep; nothing else.
Well, that's not really the purpose of DoEvents. The purposes is to immediately defer to the OS to allow it to process any messages in your apps message queue. This obviously takes a finite (but unknown) amount of time, giving the appearance of a delay., but it isn't something you have any control over
I can see that, too, it's just that DoEvents seems sometimes to fix a timing problem, and in the past I have used it when writing a file to the hard drive; it seems as if the o/s says "Ok I got it; you can get back to work now", but the file isn't completely written, or closed.
Too I figure that DoEvents on a Win/Word/VBA platform is just like the Supervisor Operations on the old mainframes - that didn't do any harm and took only a little time.
That being said, I am sure that Selenium has a built-in wait capability.
More reading at bedtime (grin) I like the idea of "fluent" because I was thinking of the worst offenders of my eight chosen search engines, and how to walk-back and measure an appropriate time interval.
Thanks again
Chris
An expensive day out: Wallet and Grimace