This morning I responded to a tweet from Tom Blackman (@itimlalake) asking how to set up a new mail notification for an additional mailbox in Outlook 2007. An additional mailbox in this context is a second mailbox on an Exchange server. Unfortunately, until 2010 Outlook did not support multiple mailboxes. While you could open and view additional mailboxes Outlook did not treat them as it does the primary mailbox. As a result things like new message notification and rules aren’t available for the additional mailboxes.
Fortunately, there is a workaround. It doesn’t look or behave the same as the built-in notification, but it is functional. The solution is to use a bit of scripting to monitor the additional mailbox’s inbox, watch for new items, and display a pop-up dialog-box when it detects one. The dialog-box is a standard Windows’ dialog, not the slick-looking notification that Outlook uses. The dialog doesn’t fade in and out, nor does it offer the ability to open or delete the message as Outlook’s notification dialog does. It will steal focus and you have to click a button to make it go away. While it’s not the best solution, it is a functional solution. Hopefully it will help Tom keep track of messages arriving in the additional mailbox.
The code for this comes in three parts.
Part 1.
Follow these instructions to add this code to Outlook.
- Start Outlook
- Click Tools > Macro > Visual Basic Editor
- If not already expanded, expand Microsoft Office Outlook Objects
- If not already expanded, expand Modules
- Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Edit the code as needed. I included comments wherever something needs to or can change
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
'On the next line change the file name and path of the sound you want to play.' Public Const SOUND_TO_PLAY = "C:\Windows\Media\Notify.wav" Public Const SND_ASYNC = &H1 Public Declare Function sndPlaySound _ Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, _ ByVal uFlags As Long) _ As Long Public Declare Function MessageBox _ Lib "User32" Alias "MessageBoxA" _ (ByVal hWnd As Long, _ ByVal lpText As String, _ ByVal lpCaption As String, _ ByVal wType As Long) _ As Long Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder ' Purpose: Opens an Outlook folder from a folder path.' ' Written: 4/24/2009' ' Author: BlueDevilFan' ' Outlook: All versions' Dim arrFolders As Variant, _ varFolder As Variant, _ bolBeyondRoot As Boolean On Error Resume Next If strFolderPath = "" Then Set OpenOutlookFolder = Nothing Else Do While Left(strFolderPath, 1) = "\" strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1) Loop arrFolders = Split(strFolderPath, "\") For Each varFolder In arrFolders Select Case bolBeyondRoot Case False Set OpenOutlookFolder = Outlook.Session.Folders(varFolder) bolBeyondRoot = True Case True Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder) End Select If Err.Number <> 0 Then Set OpenOutlookFolder = Nothing Exit For End If Next End If On Error GoTo 0 End Function
Part 2.
Follow these instructions to add this code to Outlook.
- If not already expanded, expand Microsoft Office Outlook Objects
- Right-click on Class Modules, select Insert > Class Module
- In the Properties panel click on Name and enter FolderMonitor
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Edit the code as needed. I included comments wherever something needs to or can change
- Click the diskette icon on the toolbar to save the changes
Private WithEvents olkItems As Outlook.Items Private Sub Class_Terminate() Set olkItems = Nothing End Sub Public Sub FolderToWatch(objFolder As Outlook.Folder) Set olkItems = objFolder.Items End Sub Private Sub olkItems_ItemAdd(ByVal Item As Object) Dim lngHandle As Long, lngReturn As Long sndPlaySound SOUND_TO_PLAY, SND_ASYNC MessageBox &O0, "New message arrived.", olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal End Sub
Part 3.
Follow these instructions to add this code to Outlook.
- If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Edit the code as needed. I included comment lines wherever something needs to or can change
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
- Click Tools > Trust Center
- Click Macro Security
- Set Macro Security to “Warnings for all macros”
- Click OK
- Close Outlook
- Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.
Dim objFM1 As FolderMonitor Private Sub Application_Quit() Set objFM1 = Nothing End Sub Private Sub Application_Startup() Set objFM1 = New FolderMonitor 'Edit the folder path on the next line as needed.' objFM1.FolderToWatch OpenOutlookFolder("Mailbox - TechnicLee\Inbox") End Sub
Revisions.
- Revision 1
This revision is for Ashok who asked for the ability to have different messages for different mailboxes (i.e. to have different text in the notification based on the mailbox being monitored) and to display the sender’s name in the notification. I added a third new feature, which is the ability to set different sounds for different mailboxes. Adding these features entailed adding three properties to the FolderMonitor class. Those properties are Message (used to set the message shown in the notification dialog-box), Sound (used to set the sound that will be played), and ShowSender (used to control whether the message sender is shown). All three settings are optional and use a default value if you opt not to use them. This makes the code more flexible, satisfying Ashok’s request without changing the code for those of you who don’t want the features that Ashok does.
To implement this revision, replace the code in the respective parts with this code, then close and restart Outlook. Refer to the instructions in the original post for details on how to add the code for each part.
Part 2.
Private WithEvents olkItems As Outlook.Items Private strMessage As String Private strSound As String Private bolShowSender As Boolean Private Sub Class_Initialize() 'On the next three lines edit the default values as desired.' strMessage = "New message arrived." strSound = "C:\Windows\Media\Notify.wav" bolShowSender = False End Sub Private Sub Class_Terminate() Set olkItems = Nothing End Sub Private Sub olkItems_ItemAdd(ByVal Item As Object) Dim lngHandle As Long, lngReturn As Long sndPlaySound strSound, SND_ASYNC If bolShowSender Then MessageBox &O0, strMessage & vbCrLf & "Sender: " & Item.SenderName, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal Else MessageBox &O0, strMessage, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal End If End Sub Public Sub FolderToWatch(objFolder As Outlook.Folder) Set olkItems = objFolder.Items End Sub Public Property Let Message(ByVal strValue As String) strMessage = strValue End Property Public Property Let ShowSender(ByVal bolValue As Boolean) bolShowSender = bolValue End Property Public Property Let Sound(ByVal strValue As String) strSound = strValue End Property
Part 3.
Dim objFM1 As FolderMonitor Private Sub Application_Quit() Set objFM1 = Nothing End Sub Private Sub Application_Startup() Set objFM1 = New FolderMonitor With objFM1 'The next line is optional. Delete it to use the default message. To set a message enter the text you want shown.' .Message = "New message in ..." 'The next line is optional. Delete it or set the value to False if you do not want the sender shown.' .ShowSender = True 'The next line is optional. Delete it to use the default sound. To set a sound edit name/path of the sound to play.' .Sound = "C:\Windows\Media\Notify.wav" 'The next line is not optional. Edit the folder path on the next line as needed.' .FolderToWatch OpenOutlookFolder("Mailbox - TechnicLee\Inbox") End With End Sub
- Revision 2
I created this revision for Jarratt. Jarratt wants to be able to monitor multiple folders, but for one of those folders he only wants a notification if the message came from a certain domain (e.g. gmail.com). This code builds on Revision 1 by adding that filtering capability.
To implement this revision, replace the code in the respective parts with this code, then close and restart Outlook. Refer to the instructions in the original post for details on how to add the code for each part.
Part 2.
Private WithEvents olkItems As Outlook.Items Private strMessage As String Private strSound As String Private bolShowSender As Boolean Private colFilter As Collection Private Sub Class_Initialize() 'On the next three lines edit the default values as desired.' strMessage = "New message arrived." strSound = "C:\Windows\Media\Notify.wav" bolShowSender = False Set colFilter = New Collection End Sub Private Sub Class_Terminate() Set olkItems = Nothing End Sub Private Sub olkItems_ItemAdd(ByVal Item As Object) Dim strSender As String, strDomain As String If Item.Class = olMail Then strSender = GetSMTPAddress(Item, GetOutlookVersion()) On Error Resume Next strDomain = colFilter.Item(Mid(strSender, InStr(1, strSender, "@") + 1)) On Error GoTo 0 If (strDomain <> "") Or (colFilter.Count = 0) Then sndPlaySound strSound, SND_ASYNC If bolShowSender Then MessageBox &O0, strMessage & vbCrLf & "Sender: " & Item.SenderName, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal Else MessageBox &O0, strMessage, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal End If End If End If End Sub Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String Dim olkSnd As Outlook.AddressEntry, olkEnt As Object On Error Resume Next Select Case intOutlookVersion Case Is < 14 If Item.SenderEmailType = "EX" Then GetSMTPAddress = SMTP2007(Item) Else GetSMTPAddress = Item.SenderEmailAddress End If Case Else Set olkSnd = Item.Sender If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then Set olkEnt = olkSnd.GetExchangeUser GetSMTPAddress = olkEnt.PrimarySmtpAddress Else GetSMTPAddress = Item.SenderEmailAddress End If End Select On Error GoTo 0 Set olkPrp = Nothing Set olkSnd = Nothing Set olkEnt = Nothing End Function Private Function GetOutlookVersion() As Integer Dim arrVer As Variant arrVer = Split(Outlook.Version, ".") GetOutlookVersion = arrVer(0) End Function Private Function SMTP2007(olkMsg As Outlook.MailItem) As String Dim olkPA As Outlook.PropertyAccessor On Error Resume Next Set olkPA = olkMsg.PropertyAccessor SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E") On Error GoTo 0 Set olkPA = Nothing End Function Public Sub FolderToWatch(objFolder As Outlook.Folder) Set olkItems = objFolder.Items End Sub Public Property Let Filter(ByVal strValue As String) On Error Resume Next If colFilter.Item(strValue) = "" Then colFilter.Add strValue, strValue End If On Error GoTo 0 End Property Public Property Let Message(ByVal strValue As String) strMessage = strValue End Property Public Property Let ShowSender(ByVal bolValue As Boolean) bolShowSender = bolValue End Property Public Property Let Sound(ByVal strValue As String) strSound = strValue End Property
Part 3.
Dim objFM1 As FolderMonitor Private Sub Application_Quit() Set objFM1 = Nothing End Sub Private Sub Application_Startup() Set objFM1 = New FolderMonitor With objFM1 'The next line is optional and may be repeated as many times as necessary. Enter an email domain name without the @ sign. This will restrict notifications to just those message from that domain. .Filter = "gmail.com" 'The next line is optional. Delete it to use the default message. To set a message enter the text you want shown.' .Message = "New message in ..." 'The next line is optional. Delete it or set the value to False if you do not want the sender shown.' .ShowSender = True 'The next line is optional. Delete it to use the default sound. To set a sound edit name/path of the sound to play.' .Sound = "C:\Windows\Media\Notify.wav" 'The next line is not optional. Edit the folder path on the next line as needed.' . FolderToWatch OpenOutlookFolder("Mailbox - TechnicLee\Inbox") End With End Sub
- Revision 3
I created this revision for David who asked for a knock-off that adds the ability to save message attachments to a specific folder. I implemented this new capability as an option David can activate on a per-mailbox basis. I also added the ability to display the message subject, sender’s name, or both in the notification dialog.
Part 1.
Follow these instructions to add this code to Outlook.
- Start Outlook
- Click Tools > Macro > Visual Basic Editor
- If not already expanded, expand Microsoft Office Outlook Objects
- If not already expanded, expand Modules
- Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Edit the code as needed. I included comments wherever something needs to or can change
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
'On the next line change the file name and path of the sound you want to play.' Public Const SOUND_TO_PLAY = "C:\Windows\Media\Notify.wav" Public Const SND_ASYNC = &H1 Public Declare Function sndPlaySound _ Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, _ ByVal uFlags As Long) _ As Long Public Declare Function MessageBox _ Lib "User32" Alias "MessageBoxA" _ (ByVal hWnd As Long, _ ByVal lpText As String, _ ByVal lpCaption As String, _ ByVal wType As Long) _ As Long Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder ' Purpose: Opens an Outlook folder from a folder path.' ' Written: 4/24/2009' ' Author: BlueDevilFan' ' Outlook: All versions' Dim arrFolders As Variant, _ varFolder As Variant, _ bolBeyondRoot As Boolean On Error Resume Next If strFolderPath = "" Then Set OpenOutlookFolder = Nothing Else Do While Left(strFolderPath, 1) = "\" strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1) Loop arrFolders = Split(strFolderPath, "\") For Each varFolder In arrFolders Select Case bolBeyondRoot Case False Set OpenOutlookFolder = Outlook.Session.Folders(varFolder) bolBeyondRoot = True Case True Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder) End Select If Err.Number <> 0 Then Set OpenOutlookFolder = Nothing Exit For End If Next End If On Error GoTo 0 End Function
Part 2.
Private WithEvents olkItems As Outlook.Items Private strMessage As String Private strSaveToFolder As String Private strSound As String Private bolShowSender As Boolean Private bolShowSubject As Boolean Private colFilter As Collection Private Sub Class_Initialize() 'On the next three lines edit the default values as desired.' strMessage = "New message arrived." strSaveToFolder = "" strSound = "C:\Windows\Media\Notify.wav" bolShowSender = False bolShowSubject = False Set colFilter = New Collection End Sub Private Sub Class_Terminate() Set olkItems = Nothing End Sub Private Sub olkItems_ItemAdd(ByVal Item As Object) Dim strSender As String, strDomain As String, strAdditionalInfo As String, olkAttachment As Outlook.Attachment If Item.Class = olMail Then strSender = GetSMTPAddress(Item, GetOutlookVersion()) On Error Resume Next strDomain = colFilter.Item(Mid(strSender, InStr(1, strSender, "@") + 1)) On Error GoTo 0 If (strDomain <> "") Or (colFilter.Count = 0) Then sndPlaySound strSound, SND_ASYNC If bolShowSender Then strAdditionalInfo = "Sender: " & Item.SenderName End If If bolShowSubject Then If strAdditionalInfo = "" Then strAdditionalInfo = "Subject: " & Item.Subject Else strAdditionalInfo = strAdditionalInfo & vbCrLf & "Subject: " & Item.Subject End If End If If strSaveToFolder <> "" Then For Each olkAttachment In Item.Attachments If Not IsHiddenAttachment(olkAttachment) Then olkAttachment.SaveAsFile strSaveToFolder & Format(Now, "yyyy-mm-dd-hh-nn-ss") & " " & olkAttachment.FileName End If Next End If If strAdditionalInfo = "" Then MessageBox &O0, strMessage, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal Else MessageBox &O0, strMessage & vbCrLf & strAdditionalInfo, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal End If End If End If End Sub Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E" Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant On Error Resume Next Set olkPA = olkAtt.PropertyAccessor varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID) IsHiddenAttachment = (varTemp <> "") On Error GoTo 0 Set olkPA = Nothing End Function Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String Dim olkSnd As Outlook.AddressEntry, olkEnt As Object On Error Resume Next Select Case intOutlookVersion Case Is < 14 If Item.SenderEmailType = "EX" Then GetSMTPAddress = SMTP2007(Item) Else GetSMTPAddress = Item.SenderEmailAddress End If Case Else Set olkSnd = Item.Sender If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then Set olkEnt = olkSnd.GetExchangeUser GetSMTPAddress = olkEnt.PrimarySmtpAddress Else GetSMTPAddress = Item.SenderEmailAddress End If End Select On Error GoTo 0 Set olkPrp = Nothing Set olkSnd = Nothing Set olkEnt = Nothing End Function Private Function GetOutlookVersion() As Integer Dim arrVer As Variant arrVer = Split(Outlook.Version, ".") GetOutlookVersion = arrVer(0) End Function Private Function SMTP2007(olkMsg As Outlook.MailItem) As String Dim olkPA As Outlook.PropertyAccessor On Error Resume Next Set olkPA = olkMsg.PropertyAccessor SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E") On Error GoTo 0 Set olkPA = Nothing End Function Public Sub FolderToWatch(objFolder As Outlook.Folder) Set olkItems = objFolder.Items End Sub Public Property Let Filter(ByVal strValue As String) On Error Resume Next If colFilter.Item(strValue) = "" Then colFilter.Add strValue, strValue End If On Error GoTo 0 End Property Public Property Let Message(ByVal strValue As String) strMessage = strValue End Property Public Property Let SaveToFolder(ByVal strValue As String) If strValue <> "" Then strSaveToFolder = strValue & IIf(Right(strValue, 1) = "\", "", "\") End If End Property Public Property Let ShowSender(ByVal bolValue As Boolean) bolShowSender = bolValue End Property Public Property Let ShowSubject(ByVal bolValue As Boolean) bolShowSubject = bolValue End Property Public Property Let Sound(ByVal strValue As String) strSound = strValue End Property
Part 3.
Dim objFM1 As FolderMonitor Private Sub Application_Quit() Set objFM1 = Nothing End Sub Private Sub Application_Startup() Set objFM1 = New FolderMonitor With objFM1 'The next line is optional and may be repeated as many times as necessary. Enter an email domain name without the @ sign. This will restrict notifications to just those message from that domain. .Filter = "gmail.com" 'The next line is optional. Delete it to use the default message. To set a message enter the text you want shown.' .Message = "New message in ..." 'The next line is optional. Delete it if you do not want to save the message attachments. If you include this parameter, then the attachments will be saved to the specified folder. .SaveToFolder = "C:\Users\David\Documents\" 'The next line is optional. Delete it or set the value to False if you do not want the sender shown.' .ShowSender = True 'The next line is optional. Delete it or set the value to False if you do not want the subject shown.' .ShowSubject = True 'The next line is optional. Delete it to use the default sound. To set a sound edit name/path of the sound to play.' .Sound = "C:\Windows\Media\Notify.wav" 'The next line is not optional. Edit the folder path on the next line as needed.' . FolderToWatch OpenOutlookFolder("Mailbox - TechnicLee\Inbox") End With End Sub
- Revision 4
I created this revision for Jimmy who asked for a version that would monitor the Inbox folder of every shared mailbox whose name begins with “FAX -“. The code searches through the message stores defined in the user’s Outlook profile and creates a folder monitor object for each store whose display name begins with “FAX -“. The folder monitor object will display a notification each time an item is added to the Inbox folder of the message store it’s associated with.
Part 1.
Follow these instructions to add this code to Outlook.
- Start Outlook
- Click Tools > Macro > Visual Basic Editor
- If not already expanded, expand Microsoft Office Outlook Objects
- If not already expanded, expand Modules
- Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
Public Const SND_ASYNC = &H1 Public Declare Function sndPlaySound _ Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, _ ByVal uFlags As Long) _ As Long Public Declare Function MessageBox _ Lib "User32" Alias "MessageBoxA" _ (ByVal hWnd As Long, _ ByVal lpText As String, _ ByVal lpCaption As String, _ ByVal wType As Long) _ As Long
Part 2.
Follow these instructions to add this code to Outlook.
- If not already expanded, expand Microsoft Office Outlook Objects
- Right-click on Class Modules, select Insert > Class Module
- In the Properties panel click on Name and enter clsFolderMonitor
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Edit the code as needed. I included comments wherever something needs to or can change
- Click the diskette icon on the toolbar to save the changes
Private WithEvents olkItems As Outlook.Items Private strMessage As String Private strSound As String Private bolShowSender As Boolean Private Sub Class_Initialize() 'On the next three lines edit the default values as desired.' strMessage = "New message arrived." strSound = "C:\Windows\Media\Notify.wav" bolShowSender = False End Sub Private Sub Class_Terminate() Set olkItems = Nothing End Sub Private Sub olkItems_ItemAdd(ByVal Item As Object) Dim lngHandle As Long, lngReturn As Long sndPlaySound strSound, SND_ASYNC If bolShowSender Then MessageBox &O0, strMessage & vbCrLf & "Sender: " & Item.SenderName, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal Else MessageBox &O0, strMessage, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal End If End Sub Public Sub FolderToWatch(objFolder As Outlook.Folder) Set olkItems = objFolder.Items End Sub Public Property Let Message(ByVal strValue As String) strMessage = strValue End Property Public Property Let ShowSender(ByVal bolValue As Boolean) bolShowSender = bolValue End Property Public Property Let Sound(ByVal strValue As String) strSound = strValue End Property
Part 3.
Follow these instructions to add this code to Outlook.
- If not already expanded, expand Microsoft Office Outlook Objects
- Right-click on Class Modules, select Insert > Class Module
- In the Properties panel click on Name and enter clsJimmy
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Edit the code as needed. I included comments wherever something needs to or can change
- Click the diskette icon on the toolbar to save the changes
Private colMon As Collection Private Sub Class_Initialize() Dim olkSto As Outlook.Store, objTmp As clsFolderMonitor Set colMon = New Collection For Each olkSto In Session.Stores If Left(olkSto.DisplayName, 5) = "FAX -" Then Set objTmp = New clsFolderMonitor With objTmp .FolderToWatch olkSto.GetDefaultFolder(olFolderInbox) .Message = "New message in mailbox " & olkSto.DisplayName .ShowSender = True End With colMon.Add objTmp, olkSto.DisplayName End If Next Set olkSto = Nothing End Sub Private Sub Class_Terminate() Dim intPtr As Integer, objTmp As clsFolderMonitor For intPtr = colMon.Count To 1 Step -1 Set objTmp = colMon(intPtr) Set objTmp = Nothing Next Set objTmp = Nothing Set colMon = Nothing End Sub
Part4.
Follow these instructions to add this code to Outlook.
- If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
- Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
- Click Tools > Trust Center
- Click Macro Security
- Set Macro Security to “Warnings for all macros”
- Click OK
- Close Outlook
- Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.
Dim objFM1 As clsJimmy Private Sub Application_Quit() Set objFM1 = Nothing End Sub Private Sub Application_Startup() Set objFM1 = New clsJimmy End Sub
First of all, thank you David sharing and supporting this.
I have been using this code for my coworkers and I for the past year and works a treat! It has become something we depend on and a valuable tool.
Since several of us have had our laptops replaced (some with Win10 and some with Win7) the common denominator seems to be that we all have outlook 2010. It used to be that we only received notifications for new email if outlook happened to be open at the time and since the switch to 2010, one gets flooded with notification for each email that came in overnight.
Is there a way to only monitor and send notification if an email comes in while outlook is open and to ignore emails already in the folder when opening outlook?
Thanks again, David.
Hi, Michael.
You’re welcome. Glad the solution has been useful.
What email solution are you using? Is it Exchange or something else?
We are using Exchange and it is a shared mailbox.
As I booted up this morning, I had approx. 25 notifications I had to click through.
It didn’t behave this way prior to Outlook 2010.
Hi David,
I replied almost two months ago but it still says waiting for moderation.
We are using Exchange and it is a shared mailbox.
Thanks
Hi, Michael.
The replies don’t appear until I approve them, which I don’t normally do until I’m ready to respond. Sorry to have taken so long to get back to you. I do this as I have time and energy and lately I haven’t had a lot of either.
Which version of the code are you using, that in the original post or one of the revisions? If it’s the latter, which revision?
Hi David,
I cannot seem to get your script to work. I have followed your instructions and renamed the mailbox in part 3. However I am getting a run time error 91. It highlights this line in yellow objFM1.FolderToWatch OpenOutlookFolder(“Mailbox – Accommodation\Inbox”)
Any ideas?
Hi, Jason.
My first thought is that the path to the inbox folder may be incorrect. Add the code below to what you already have. Select the target folder, then run that macro. It will display the path to the selected folder. Use that path in the solution.
Cheers David,
That did the trick.
Works perfectly!
Hey, a quick question if thats OK?
I already have a script in ThisOutlookSession, so I have:-
“ThisOutlookSession”
“Module 1 – (your part 1)”
“FolderMonitor (your part 2)”
So does your part 3 go into a new module, or into the end of Module 1?…..or the end of my already populated “TheOutlookSession” – which I dont think will work.
Still new to VB, so apologies 🙂
thanks, Danny.
Hi, Danny.
Is the code in ThisOutlookSession from one of my solutions? If so, which one? If not, then I’ll need to see that code to figure out how to integrate my code from this solution that must go into ThisOutlookSession with it.
Hey David,
Thanks for your reply.
No its one that I wrote, with help from google. Its a script that forwards an appointment when fired, to an email account, and then dismisses the appointment to make it look like a normal email. I am still very new to VB so I need to figure out how to incorporate your part 3 into this:-
======================================
Hi, Danny.
You can just add my code to what you already have. Something like this
Hey,
Sorry for the delay and thanks for your comment.
Its not working unfortunately 😦 Think it needs a slight shuffle,
Thanks,
Danny.
Danny,
Did you edit the path to the folder you want to monitor on line #10?
Hi David,
At first, thank you so much for the vba codes.
I’m using the revision 1 and it works great. However there is a small problem here: the vba will stop working whenever I receive an email from Microsoft Exchange about Undeliverable email or Delivery Delayed. Please kindy advise how to fix it. All I had to do is restart the Outlook 😦
Hi, Vietpl.
You’re welcome!
In part 2 of the code change the olkItems_ItemAdd subroutine to the version below. That should fix the issue.
Hi David,
I will try that and update you on the outcome.
By the way, I’m trying to figure it out by using the codes below (edited from yours) to prevent the new in coming emails with specified subjects which contain specified keywords: “Undeliverable:”, “Delivery delayed:”, Backup on”, “Automatic reply”, “Out of office:” but seem not working, can you please help to check what is wrong for me? Sorry for my bad English.
Private Sub olkItems_ItemAdd(ByVal Item As Object)
Dim lngHandle As Long, lngReturn As Long, bolUnd As Boolean, bolOOO, bolOOd, bolOOe, bolOOf As Boolean
bolUnd = InStr(1, Item.Subject, “Undeliverable:”)
bolOOO = InStr(1, Item.Subject, “Delivery delayed:”)
bolOOd = InStr(1, Item.Subject, “Backup on”)
bolOOe = InStr(1, Item.Subject, “Automatic reply”)
bolOOf = InStr(1, Item.Subject, “Out of office:”)
If (Not bolUnd) And (Not bolOOO) And (Not bolOOd) And (Not bolOOe) And (Not bolOOf) Then
sndPlaySound SOUND_TO_PLAY, SND_ASYNC
If bolShowSender Then
MessageBox &O0, strMessage & vbCrLf & “Sender: ” & Item.SenderName, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal
Else
MessageBox &O0, strMessage, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal
End If
End If
End Sub
Hi David,
I will try it and let you know the outcome. By the way I’m trying to edit the part 2 as below to prevent notification popup for new email with specified subjects which contains one of these keywords: Undeliverable, Delivery delayed, Backup on, Automatic reply, Out of office. But it seems not working. Can you please help to check what is wrong? Thank you very much.
Private Sub olkItems_ItemAdd(ByVal Item As Object)
Dim lngHandle As Long, lngReturn As Long, bolUnd As Boolean, bolOOO, bolOOd, bolOOe, bolOOf As Boolean
bolUnd = InStr(1, Item.Subject, “Undeliverable:”)
bolOOO = InStr(1, Item.Subject, “Delivery delayed:”)
bolOOd = InStr(1, Item.Subject, “Backup on”)
bolOOe = InStr(1, Item.Subject, “[Daiichi]”)
bolOOf = InStr(1, Item.Subject, “[REC]-SLA OVERDUE”)
If (Not bolUnd) And (Not bolOOO) And (Not bolOOd) And (Not bolOOe) And (Not bolOOf) Then
sndPlaySound SOUND_TO_PLAY, SND_ASYNC
If bolShowSender Then
MessageBox &O0, strMessage & vbCrLf & “Sender: ” & Item.SenderName, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal
Else
MessageBox &O0, strMessage, olkItems.Parent.Name, vbInformation + vbOKOnly + vbSystemModal
End If
End If
End Sub
Hi David,
Thank you very much! It works very well.
Here I have another concern, I want to prevent the notification of Undeliverable, Delivery delayed emails from Microsoft Outlook (You helped me out this problem), and also want to prevent notification for new emails which the subject contains specified keywords likes “Backup on”, “Automatic reply”, “Out of office”. Can you please help me to make it happens?
Below is my codes but seems it doesn’t work 😦
Hi, Allan.
You’re welcome.
On line #2 declare all the variables that begin with bol to be of type Boolean just as bolUnd and bolOOf are.
Hi David,
I still don’t get it. Please show me which line need to be fixed?
Thank you very much!
Hi, Allan.
Change line #2 from
to
Hi David,
It’s working perfect now. Thank you 🙂
I have another question, is there any way to mark those new emails with specified subjects as read by VBA?
Hi, Allan.
Try this version.
Hi
Im searching for the Path. How can i find out the correct path to my
objFM1.FolderToWatch OpenOutlookFolder(“path i need to find”)
Is that path stored anyware in the Outlook?
Thanks a lot
Novski
Hi, Novski.
Add this code to that you already have. Once you’ve done that, select a folder and run this macro. It will display the path to the selected folder.
Hi
I am facing issue here , getting error and VB is highlighting “Private WithEvents……”
Compile error:
Only Valid in object module
Please help,
Hi, Santosh.
Based on the error it sounds like you put the code for part #2 in a regular module rather than in a class module. Please double-check the instructions for part #2.
Hi
I have problems with the revision 1 on part 2 point 3:
“In the Properties panel click on Name and enter FolderMonitor”
I can’t follow…
Maybe my Outlook 2013 is different?
The error i get is this:
On German: Fehler beim Kompilieren: Benutzerdefinierter Typ anstelle eines Projekts erwartet.
Google translate: Compile Error: User-defined type rather than a project expected.
Hi, Novski.
Do you have a ClassModule named FolderMonitor?
Hi David, Thanks for the reply. I can not folow that part.
What i did is to Rename the Projectname to FolderMonitor. I can’t find a propterties panel or a field named “Name” to enter FolderMonitor…
Is Outlook 2013 different?
Thanks a lot.
Hi, Novski.
In the VB editor, select the class module then press F4 to open the Properties panel. Click i the Name field and change the name.
Hi David,
Thanks so much for this!
I’ve been testing the different revisions trying to understand the code (as I’m not a VBA coder).
My question relates to Revision 4:
I’ve tried to implement a version of this script, changing the code
If Left(olkSto.DisplayName, 5) = “FAX -” Then
To match the folder structure of my shared mailboxes – however when I attempt to execute the code whenever I get a match (confirmed via a msgbox) the system generates a
“run-time error ‘438’:
Object doesn’t support this property or method”
When I debug the error it takes me to:
Private Sub Application_Startup()
Set objFM1 = New clsJimmy
End Sub
Any help would be greatly appreciated. Sorry if this has come up before but I was unable to find it in the previous comments….
Thanks,
Chris
Hi, Chris.
Based on your explanation, my best guess is that the folder path being passed is invalid. You mention that you edited the line
How did you change it? Also, please tell me more about the shared folders. Are they public folders or folders in another mailbox?
Hi David,
The code change is attached:
For Each olkSto In Session.Stores
MsgBox (Left(olkSto.DisplayName, 11))
If Left(olkSto.DisplayName, 11) = “App Support” Then
MsgBox (“enter If statement”)
I used the messages to debug the code and can confirm that we enter the ‘IF’ statement – followed by the error described previously.
In terms of the folders I’d like to monitor it’s the inboxes of multiple shared mailboxes (which all start with the “App Support” naming convention).
Thanks,
Chris
Hi David,
Just discovered the VBA ‘Debugger’ mode and identified that the last statement that executes prior to the error is (in the clsFolderMonitor Class):
Private Sub Class_Terminate()
Set olkItems = Nothing
End Sub
Thanks,
Chris
Chris,
The code in the Class_Terminate sub cannot possible cause the error. We need to dig a little deeper. Since you’ve discovered the debugger here’s what I’d like you to do.
Hi David,
I’ve followed your instructions and the code that seems to be causing the error is
With objTmp
.FolderToWatch olkSto.GetDefaultFolder(olFolderInbox)
Also prior to the error if I hover my mouse pointer over olFolderInbox from the above statement it shows “olFolderInbox = 6”
This is the last code to execute prior to the Class_Terminate sub (as described earlier)
Thanks,
Chris
Hi David,
I’ve been having a look at the
Store.GetDefaultFolder Method on the MS website:
https://msdn.microsoft.com/en-us/library/office/ff869924.aspx
and think the issue may be that we’re still running Outlook 2007… the documentation states that it was added form Outlook 2010. Could this be the issue?
Thanks,
Chris
Hi, Chris.
Yes, that would be the issue. I don’t know if I still have Office 2007 installed anywhere. I’ll check and see. If I do, then I’ll see about creating a version of this that works with it.
Hi David,
Just to let you know I think I may have stumbled onto a solution (of sorts)…
After identifying that the code that was causing the error was:
.FolderToWatch olkSto.GetDefaultFolder(olFolderInbox)
I reverted to using the OpenOutlookFolder function (from previous revisions)
.FolderToWatch OpenOutlookFolder(olkSto.DisplayName & “\Inbox”)
replaced
.FolderToWatch olkSto.GetDefaultFolder(olFolderInbox)
and it seems to be working now – I’ll complete some more vigorous testing and let you know if there are any issues.
Thanks again,
Chris
Hi, Chris.
Clever workaround. Please let me know how it goes.
Hi David
I have another question. How can i find out the correct path to my
objFM1.FolderToWatch OpenOutlookFolder(“path i need to find”)
I can’t find out where il find that…
Thanks a lot
Novski
VB Sound Alert help for two shared mail box this is for only one Shared mail box help I want for all the two not for my personal mail is anyone can help me out
Hi, Selvaraj Nadar.
You can monitor as many mailboxes as you want. To monitor additional mailboxes modify the code in part 3 to something like this
Hello David,
I’m french, so not sure on my english.
I found your post while I wanted to have an alert when a new mail arrived in the inbox from other mailboxes.
I used office 2007, with exchange 2007.
I lunch outlook with my profil, and I had added 10 mailboxes with tools>account parameter>modifiy>more parameter>avanced.
We use personnal email like : john.doe@my.domain.fr
and project email : thebigtower@my.domain.fr
And everybody have access to project email.
So I wanna have an alert when on each project email, a new mail incomme in the inbox.
Salut, likum.
Modifier la ligne 10 du code dans la Partie 3. Sur cette ligne, modifier le chemin “Mailbox – TechnicLee\Inbox” sur le chemin de la boîte de réception de la boîte aux lettres Projets. Si vous ne savez pas quel est le chemin, puis ajouter ce code pour que vous avez déjà. Une fois que vous avez fait cela, sélectionnez la boîte de réception de la boîte aux lettres Projets et exécutez la macro “GetFolderPath”. Il affichera le chemin vers ce dossier. Copiez et collez ce chemin dans la ligne 10 de la macro de notification.
Hello David,
Thank’s :). It’s work.
For me it’s :
objFM1.FolderToWatch OpenOutlookFolder(“\\Boîte aux lettres – my_mailbox_name\Boîte de réception”) because I’m in french.
I have juste one more question :
How can I prompt the mailbox name, which recieve the email, in the MessageBox ?
Hello twice,
I found it.
I juste change in part2 :
MessageBox &O0, “De : ” & Item.SenderName & vbCrLf & “A : ” & Item.To, “Nouveau mail”, vbInformation + vbOKOnly + vbSystemModal
Thank’s for all.
You are welcome!
When I open outlook I get this message error. (defined type not defined) and box opens with code.
Dim objFM1 As FolderMonitor
Private Sub Application_Quit()
Set objFM1 = Nothing
End Sub
Private Sub Application_Startup()
Set objFM1 = New FolderMonitor
‘Edit the folder path on the next line as needed.’
objFM1.FolderToWatch OpenOutlookFolder(“Mailbox – TechnicLee\Inbox”)
End Sub
And I changed TechnicLee to match the mailbox I need.
Hi, Alan.
Did you also add the FolderMonitor class module?
Is there a way to add a button to the notification window to allow you to open the email?
Hi Josh,
Excellent script. I have been using this for a while.
I have another piece of code that I would like to incorporate so that rather than creating a message box, a different email is sent to my main inbox. This would stop the need to click “okay” on the pop-up and I can automatically delete the alert messages nightly.
I have
> Sub SendNew(Item As Outlook.MailItem)
>
> Dim objMsg As MailItem
>Set objMsg = Application.CreateItemFromTemplate(“C:\Alert.oft”)
>
>objMsg.Recipients.Add “Me@foo.com”
>
>objMsg.Send
>
>End Sub
Which runs fine as a script in Outlook rules and alerts, but I am unsure of how to incorporate it into your code which monitors another mailbox.
Never mind, silly me. I just got this working. I added
>Dim objMsg As Outlook.MailItem
>Set objMsg = Application.CreateItemFromTemplate(“C:\Alert.oft”)
>
>objMsg.Recipients.Add “Me@foo.com”
>
>objMsg.Send
To the olkItems_ItemAdd private sub and commented out the MessageBox and sndPlaySound lines.
Thanks,
B