How to check Outlook inbox automatically?

If You don’t want to miss the expected email among tons of other messages, I invite You to this article, where I will show You how to check Outlook inbox automatically.

How to check Outlook inbox automatically tom hanks

Total automation idea!

Often You wait for certain email with attachment, which is input for your report. Your report is doing automatically, so why should You bother about anything, manually? Why not start the whole process as soon as the specified email arrives?

OK, this went too far, because this automatic process can interrupt what You are doing in that moment. But You can have a message box with information at least, that awaited email is already in the inbox!

First things first

This time You are not going to open Excel application, but Outlook. Yes, Outlook! Add to ribbon Developer tab, if You don’t have it, and go to the VBA editor (or just simply press ALT + F11).

In this case You are going to code in ThisOutlookSession, not as usual in Module.

check Outlook inbox automatically thisoutlooksession

You will need 2 sub’s – startup of Outlook application and adding item into inbox folder event.

Sub at the startup of Outlook

You have to set your inbox and its emails (.Items) into variable at the start up of Outlook (Application_Startup). Code needs to know, which emails to check and where.

Option Explicit
Private WithEvents inboxItems As Outlook.Items

Private Sub Application_Startup()
  Dim outlookApp As Outlook.Application
  Dim objectNS As Outlook.NameSpace
  
  Set outlookApp = Outlook.Application
  Set objectNS = outlookApp.GetNamespace("MAPI")
  Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Sub for adding item event

Then create sub, which is working every time item added into inbox (inboxItems_ItemAdd(ByVal Item As Object)). In this case just check the subject of email. If it’s correct pop out the message box with information about sender email address, whole email subject and the content of email.

Private Sub inboxItems_ItemAdd(ByVal Item As Object)

Dim MessageInfo As String

If TypeName(Item) = "MailItem" Then
    If InStr(1, Item.Subject, "Your report input") Then
        MessageInfo = "" & _
        "Sender : " & Item.SenderEmailAddress & vbCrLf & _
        "Subject : " & Item.Subject & vbCrLf & _
        "Message Body : " & vbCrLf & Item.Body
        MsgBox MessageInfo
    End If
End If

End Sub

There are million other options what You can do after detecting the input email, which You are interested in, but on another occasion.

End words

This was just simple example how to check Outlook inbox automatically. You can use this and develop for your purposes or wait for the next article, where I’ll present You other approaches to similar cases.

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

Leave a Reply

Your email address will not be published. Required fields are marked *