Please, tell me about DoEvents command

Hello,
I have a question.
Could you please describe a command:
DoEvents
When it used?
Many Thanks,
John
[147 byte] By [John _Smith] at [2007-11-15 18:34:07]
# 1 Re: Please, tell me about DoEvents command
When CPU is occupied with your program, computer cannot do anything else. For example if you have a code

Do While 1 = 1
Loop

You will never escape from this loop and won't be able to stop your program. If you add DoEvents

Do While 1 = 1
DoEvents
Loop

The DoEvents command gives some time to the CPU to do other tasks. In cas of this loop you will be able to click Stop button.

Usually when you run loops that last long time you want to include the DoEvent statement to do something else while your program is running.

Iouri Boutchkine
iouri@hotsheet.com
Iouri at 2007-11-10 0:33:54 >
# 2 Re: Please, tell me about DoEvents command
In addition to what Iouri said, you can use DoEvents to give your user a chance to cancel some long processes:

option Explicit
dim bCancelProcess = false

private sub SomeLongProcess()
do while somecondition = true and bCancelProcess = false
'some code to process
DoEvents 'this will allow the caption of button-click
loop
end sub

private Sub Command1_Click()
bCancelProcess = true
end sub
DSJ at 2007-11-10 0:34:47 >
# 3 Re: Please, tell me about DoEvents command
Does that mean that I could use the DoEvents command to make VBA wait for my e-mail to be sent before going back to the start of the loop?

Declarations

private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (byval hWnd as Long, byval lpOperation as string, _
byval lpFile as string, byval lpParameters as string, byval lpDirectory as string, _
byval nShowCmd as Long) as Long
dim Email, Subj, Txt, lastrow


for r=1 to Lastrow
URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Txt & "&content-type=text/html"
' Execute the URL (start the default email client)
ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus
' Wait two seconds before simulating Keystrokes Alt-S to send the message
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "%s"
next r
Rob Phillips at 2007-11-10 0:35:47 >
# 4 Re: Please, tell me about DoEvents command
No you cannot do that. DoEvents does not make your computer to wait. It just allows other processes to talk to CPU.
If you want to wait until your process is over you have to know the process handle. You can do it if you start your process with Shell command or use Window Script Host.
Here is a small example

Function MyFunc()
Dim myObject As Object
Dim RetVal As Variant

Set myObject = CreateObject("WScript.Shell")
RetVal = myObject.Run("c:\windows\notepad.exe", 1, True)
MsgBox "Notepad is closed"

End Function

'TRue - wait until proc is over
'False don't wait

Iouri Boutchkine
iouri@hotsheet.com
Iouri at 2007-11-10 0:36:49 >