I have the macro which reads the mailitems of new email in the inbox(Outlook 2016) and pops up msg box with subject and time, this works perfectly fine. However, it does not work if the msgbox is active and meanwhile a new email arrives the mailbox. Is there any way by which it can immediately pop up the next msgbox for the latest email?
I tried running the macro manually by adding it to the custom ribbon but that didn't work as it is private function.
Option Compare Text
Private WithEvents myOlItems As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
If Msg.subject Like "*abc.com*" Then
MsgBox "This is a priority email" & vbCrLf & vbCrLf & "Subject: " & Msg.subject & vbCrLf & "At: " & Msg.SentOn, vbOKOnly, "Priority Email" 'Msg.Subject & vbCrLf & Msg.Body
ElseIf Msg.Body Like "*abc.com*" Then
MsgBox "This is a priority email" & vbCrLf & "Subject: " & Msg.subject & vbCrLf & "At: " & Msg.SentOn, vbOKOnly, "Priority Email" 'Msg.Subject & vbCrLf & Msg.Body
ElseIf Msg.SenderEmailAddress Like "*abc.com*" Or Msg.CC Like "*abc.com*" Then
MsgBox "This is a priority email" & vbCrLf & "Subject: " & Msg.subject & vbCrLf & "At: " & Msg.SentOn, vbOKOnly, "Priority Email" 'Msg.Subject & vbCrLf & Msg.Body
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Msg.Categories = "Priority Email"
followed byMsg.save
. Change your view if necessary to display Categories. – niton