You are currently offline, waiting for your internet to reconnect

Definition of DoEvents in Visual Basic for Applications

This article was previously published under Q118468
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
The DoEvents function surrenders execution of the macro so that theoperating system can process other events. The DoEvents function passescontrol from the application to the operating system. Some instances inwhich DoEvents may be useful include the following:
  • Hardware I/O
  • Delay Loops
  • Operating System Calls
  • DDE Deadlocking
This article also discusses potential problems associated with the DoEventsfunction.

Hardware I/O

If your code waits for an input from any I/O device, the DoEvents functionspeeds up the application by multitasking. As a result, the computer doesnot seems to pause or stop responding (hang) while the code is executing.

   Open "com1" For Input As #1   Input #1, x   Do Until x = Chr(13)   DoEvents   '...   '...   Input #1, x   Loop				

Delay Loops

In a delay loop, the DoEvents function can allow the CPU operating systemto continue with any pending jobs.

   X = Timer()   Do While X + 10 > Timer()        DoEvents    Loop				

Operating System Calls

When Visual Basic calls the operating system, the operating system mayreturn the control even before processing the command completely. Doing somay prevent any macro code that depends on an object generated by the callfrom running. In the example below, the Shell function starts the MicrosoftWord application. If Word is not yet open, any effort to establish a DDElink to it will halt the code. By using DoEvents, your procedure makes surethat an operation, such as Shell, is completely executed before the nextmacro statement is processed.

   z% = Shell("WinWord Source.Doc",1)   DoEvents   ...   ...				

DDE Deadlocking

Consider a situation in which a Visual Basic macro calls an applicationthat is waiting for a second application to get some data. If the macrodoes not give control to the second application, the result is a deadlock.In DDE conversations between multiple applications, using DoEvents removesthe possibility of this type of deadlocking.

Problems Associated with DoEvents

  • Using too many nested DoEvents statements may deplete the stack space and therefore generate an "Out of Stack Space" error message. This error is referring to the application stack space allocated to the Microsoft Excel application.
  • Make sure the procedure that has given up control with DoEvents is not executed again from a different part of your code before the first DoEvents call returns; this can cause unpredictable results.
  • Once DoEvents relinquishes control to the operating system, it is not possible to determine when Microsoft Excel will resume the control. After the operating system obtains control of the processor, it will process all pending events that are currently in the message queue (such as mouse clicks and keystrokes). This may be unsuitable for some real- time data acquisition applications.
For more information about DoEvents, click the Search button in Helpand type:
Sendkeys keystroke Wait XL98 XL97 XL7 XL5 XL

Article ID: 118468 - Last Review: 10/11/2006 01:42:01 - Revision: 1.2

  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 98 for Macintosh
  • KB118468