0
votes

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
1
You have blocked the code by adding a MsgBox. One possible method to allow the code to complete, so it can process subsequent mail, is to instead add a category to the incoming mail.niton
@niton: I am really sorry as I am not very good with VBA and I could not understand "adding category". Can you elaborate little more?Nitesh Singh
Replace MsgBox code with Msg.Categories = "Priority Email" followed by Msg.save. Change your view if necessary to display Categories.niton

1 Answers

0
votes

For a prompt that does not block ItemAdd code:

dim priorityMail as mailitem

Set priorityMail = CreateItem(olMailItem)
With priorityMail
    .Subject = This is a priority email & Msg.subject
    .Body = "At: " & Msg.SentOn & vbCrLf & vbCrLf & Msg.Body
    .display
End With