Trying to check for the absence of mail.

Not open for further replies.

Lucas Lichte

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Office 365 Exchange
I'm trying to check for the absence of an email in a folder. I have a bot sending to this folder every 15 minutes. I want to check every 30 minutes to see if I've gotten an email in the last 30 minutes, and if I haven't, pop up a message box. The problem I'm running into is that I can't figure out how VBA for Outlook does Dates, and I can't find a reference.

The called Macro I'm trying to write is:

Sub Check_GWAAR()
Dim myOlItems As Object
Dim compareDate As Date
Dim sentDate As Date
Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("Test@GWAAR").Items
Set compareDate = DateAdd("m", -30, Now)
Set sentDate = myOlItems.Items(1).ReceivedTime
If sentDate < compareDate Then MsgBox ("Check the GWAAR Test VM!")
End Sub

It's telling me that the variables need to be set to objects. When I set them to objects, it tells me the Set statements don't evaluate to objects.


Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Try searching for something like VBA date format excel - there are a lot of samples for Excel and most date functions work the same. Even VB date stuff is the same. has a chart of codes.

This is months:
Set compareDate = DateAdd("m", -30, Now)

I think i would use

correction: you need to use decimals / fraction of a day when using time or now - 24 hours = 1.
if olItem.recievedtime > now - .03 then
msgbox "It didn't arrive"
end if

Maybe like this: (i did not test it)

Public Sub CheckMail()
  Dim objNS As NameSpace
  Dim objFolder As MAPIFolder
  Dim olItem As MailItem, olItems As Items
  Set objNS = GetNamespace("MAPI")
  Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
  Set olItems = objFolder.Items
  olItems.Sort "ReceivedTime", True
  Set olItem = olItems.Item(1)
'do whatever
  If olItem.ReceivedTime < Now - 0.02 Then
  MsgBox olItem.subject & " It didn't arrive"
  End If
End Sub
What are you using as a timer? You can run it when a reminder fires:
Not open for further replies.