0

I receive an email report twice every workday. Sometimes the machine that sends these reports crashes and no emails are sent out. What I am wanting to do is use some Outlook VBA to check if an email hasnt arrived at 12:15 and 17:05.

Finding an email that is there is fairly easy, but finding one that isnt is making me scratch my head a bit. I have a class module set up right now (I assume that would be the way to go) and have the code for what I want to do if no email has been received, but cannot figure out how to go about checking for the email at those times. It might be something simple, but have not really scripted in Outlook VBA before, so am not sure where to start.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bmgh1985
  • 779
  • 1
  • 14
  • 38
  • I'm wondering why you need to use VBA for this? Can't you simply check your inbox, sort by date, and *see for yourself* that the email is (or isn't) there? otherwise, what information do you know about the email (sender, subject line, etc.)? You could certainly do something to look for an email with certain parameters. – David Zemens May 30 '14 at 15:58
  • This is because I usually have another 50 or so emails that require attention already by lunchtime and then the second one is after I finish on an evening. Have cobbled together a workaround for now where I monitor daily tasks I have created and check for the emails arriving in the minute before it fires, however its not quite working yet. A constant watch would be better (akin to FileSystemWatcher in .net) but not sure if this can be accomplished in outlook VBA – bmgh1985 May 30 '14 at 16:20
  • **what information do you know about the email (sender, subject line, etc.)?** – David Zemens May 30 '14 at 16:22
  • Sender is always the same, attachment filename is always the same. Subject is same except the date changing (just using Like xxx* instead of date handling) – bmgh1985 May 30 '14 at 16:24
  • I still think you can just sort inbox by sender and either it's there or it's not, but you know *enough* about the email that you could certainly do something in VBA to run some procedure(s) [at a specific time each day](http://www.mrexcel.com/forum/excel-questions/731321-run-outlook-visual-basic-applications-macro-specific-time-day.html), then use VBA to query items by sender, and check the item's `.ReceivedTime`, etc. – David Zemens May 30 '14 at 16:38
  • That is quite similar to my workaround actually. Whole point of the emails though is that when they work I can just ignore them. Its only when they dont that I need to care about what they are doing and to me the whole point of vba is to automate repetitive tasks that can be done in the background without user interaction, so yes I could do it manually but it makes more sense to automate. – bmgh1985 May 30 '14 at 16:48
  • well it's clearly possible to do this, then. Are you expecting me to write the code for you? – David Zemens May 30 '14 at 17:07
  • No as I said I have code that works on this principle in my workaround method, but I think there may well be a more direct method using something along the lines of class modules firing at an ontime event that I am missing (which is what my question is asking) rather than having to create a task etc. by code to fire and check – bmgh1985 May 30 '14 at 18:10
  • Outlook doesn't have an `.OnTime` method like Excel (unfortunately). Here is an answer that does something similar for Outlook: http://stackoverflow.com/questions/12257985/outlook-vba-run-a-code-every-half-an-hour So you won't need to create a task necessarily but you will need to use code similar to this to replicate that `OnTime` functionality. – David Zemens May 30 '14 at 18:28
  • Thats more like it. Was wondering why I couldnt find OnTime in outlook. Will try this out next week and see what I come up with. Thanks – bmgh1985 May 30 '14 at 18:31

1 Answers1

1

The method pointed out in a comment.

Outlook VBA - Run a code every half an hour

Outlook VBA - Run a code every half an hour with outlook 2010 64 bits

A possibly simpler alternative. Set a recurring task with a reminder.

In ThisOutlookSession

Private Sub Application_Reminder(ByVal Item As Object)

If Item.Class = olTask Then
    If InStr(Item.Subject, "subject") > 0 Then
        ReminderUnreceivedMail
    End If
End If

End Sub

Sub ReminderUnreceivedMail()

Dim Itms As Items
Dim srchSender As String
Dim srchSubject As String

Set Itms = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
srchSender = "sender"
srchSubject = "subject"

Set Itms = Itms.Restrict("[SenderName] = 'sender' And [Subject] = 'subject' And [SentOn] > '" & Format(Date, "yyyy-mm-dd") & "'")

If Itms.count = 0 Then
    MsgBox "No " & srchSubject & " email on " & Format(Date, "yyyy-mm-dd")
End If

Set Itms = Nothing

End Sub
Community
  • 1
  • 1
niton
  • 8,771
  • 21
  • 32
  • 52