For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
Method 1: Use an Empty For...Next LoopA disadvantage to this method is that there is no direct way to determine the exact amount of time that the program takes to run the loop. The amount of time depends upon the speed of your computer. The following code runs a loop that does nothing but take up some time.
For iCount = 1 to 1000
Method 2: Use an API Call to Suspend Word ExecutionUse an API call to suspend the execution of Word for a fixed amount of time.
The Kernel32 contains a function that pauses a program's execution for a specified amount of time, specified in milliseconds. For you to use the function, it must first be declared in the General Declarations section of the module in which it will be used:
Declare Sub Sleep Lib "kernel32" Alias "Sleep" _
(ByVal dwMilliseconds As Long)
Sleep 1000 'Implements a 1 second delay
Method 3: Use the OnTime MethodUse the OnTime method to set an amount of time to pause. The OnTime method uses the following syntax:
This example runs the macro named "MyDelayMacro" 15 seconds from the time the example is run.
' Pause for 15 seconds.
Application.OnTime When:=Now + TimeValue("00:00:15"), _
Public Sub MyDelayMacro()
' Place your delayed macro commands here.
MsgBox "This macro runs after 15 seconds."
ID d'article : 212667 - Dernière mise à jour : 11 oct. 2006 - Révision : 1