5

I'm trying to run Outlook code 10 seconds after an email is received.

I tried using application.wait but it appears that you cannot do this with Outlook.

How do I pause Outlook for a given amount of time?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Lererferler
  • 287
  • 4
  • 19

4 Answers4

7

You can create a Sub that will mimic the Application.Wait, something like.

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'For 64-Bit
'Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Sub Pause(intSeconds As Variant)
  ' Comments: Waits for a specified number of seconds
  ' Params  : intSeconds      Number of seconds to wait
  ' Source  : Total Visual SourceBook

On Error GoTo PROC_ERR

    Dim datTime As Date
    datTime = DateAdd("s", intSeconds, Now)
    Do
        ' Yield to other programs (better than using DoEvents which eats up all the CPU cycles)
        Sleep 100
        DoEvents
    Loop Until Now >= datTime
PROC_EXIT:
    Exit Sub

PROC_ERR:
    MsgBox "Error: " & Err.Number & ". " & Err.Description, , "Pause Method"
    Resume PROC_EXIT
End Sub

To call this you could use Pause 3

PaulFrancis
  • 5,748
  • 1
  • 19
  • 36
  • For some reason it isn't letting me use the sleep function either so that's a no go. Any ideas why it is doing this? – Lererferler May 12 '15 at 16:17
  • You might need to include the library to the sleep function. Try the edited code. – PaulFrancis May 12 '15 at 16:19
  • Perfect, it didn't work before you edited the code but it does now. Thank you very much. May I ask what the issue was? – Lererferler May 12 '15 at 16:25
  • Windows provides an API function named `Sleep` that suspends the current process thread. Without including the declaration the compiler does not know what to do with Sleep. First I did not include the explicit declaration of the Sleep function. Now that we have added it, it works. – PaulFrancis May 12 '15 at 16:48
  • But where did the "Wait" go to? – pashute Jan 05 '17 at 21:36
0

Here's a simple way:

    T0 = Now + TimeValue("0:00:10")
    Do Until Now > T0
    Loop
procpy
  • 108
  • 1
  • 1
  • 7
  • 1
    I don't think this is a good idea, this is a [busy wait](https://en.wikipedia.org/wiki/Busy_waiting), you should put some [sleep](http://stackoverflow.com/questions/469347/is-there-an-equivalent-to-thread-sleep-in-vba) there. – agold Oct 22 '15 at 18:15
0

Throw a DoEvents in this and it'll be okay

T0 = Now + TimeValue("00:00:10")

Do Until Now > 10

  DoEvents

Loop
Eric Hofer
  • 65
  • 7
0

I'm not sure the need for complicated function..

Try This:

#If VBA7 Then
'Code is running VBA7 (2010 or later).

     #If Win64 Then
     'Code is running in 64-bit version of Microsoft Office.
      Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
     #Else
     'Code is running in 32-bit version of Microsoft Office.
      Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
     #End If

#Else
'Code is running VBA6 (2007 or earlier).

#End If

Sub Test()
 Debug.Print Now
 Sleep 10000
 Debug.Print Now
End Sub
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57