Pause an outlook vba code for 5-10 seconds and after run the script
I have a vba code that I have added as an outlook rule, so every time when i get daily email with a specific subject, outlook starts to run script. However,when email just received, outlook starts to run vba code immediately picking up previous email, probably because the email was just received and not moved to a specific folder yet. I tried to use Application.Wait (Now + TimeValue("0:00:5"))
and Outlook.Application.Wait (Now + TimeValue("0:00:5"))
just before grabbing the email lineSet oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")
and it doesn't work, vba shows an error Object doesn't support this property or method
Here is the beginning of my code:The error Application.Wait (Now + TimeValue("0:00:5"))
Sub ExportOutlookTableToExcel()
Dim oLookInspector As Inspector
Dim oLookMailitem As MailItem
Dim oLookWordDoc As Word.Document
Dim oLookWordTbl As Word.Table
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlWrkSheet As Excel.Worksheet
Dim Today As String
Today = Date
Application.Wait (Now + TimeValue("0:00:5"))
'Grab Email Item
Set oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")
Set oLookInspector = oLookMailitem.GetInspector
Set oLookWordDoc = oLookInspector.WordEditor
Outlook has no Application.Wait
You can workaround this with a Do
loop, a Timer
and DoEvents
:
Public Sub Sleep(ByVal SleepSeconds As Single)
Dim Tmr As Single
Tmr = Timer
Do While Tmr + SleepSeconds > Timer
DoEvents
Loop
End Sub
And call it like Sleep 5
instead of your Application.Wait
.