PDA

View Full Version : VBA Script Help - Display a custom prompt if there is unread mail in a shared mailbox



ITApprentice
03-11-2019, 05:39 AM
A request has been made to display a notification for unread mail that is in a shared mailbox.

My initial response to the customer was to setup an email rule wasn't sufficient, the user does not get a pop up alert that stays on the screen.

After googling, you can configure a VBA script to pop up when there is more than X unread mail in an inbox. Unfortunately, I have not been able to get it to work for shared mailboxes, though it seems fairly possible.



Private objInbox As Outlook.Folder
Private WithEvents objItems As Outlook.Items
Private lUnreadItemCount As Long

Private Sub Application_Startup()
Set objInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set objItems = objInbox.Items

lUnreadItemCount = 0
Call CountUnreadEmails(objInbox, lUnreadItemCount)

'If there are more than 10 unread emails
If lUnreadItemCount > 10 Then
MsgBox "Too many unread emails in Inbox!" & vbCr & "Please deal with them as soon as possible!", vbExclamation + vbOKOnly, "Check Unread Emails"
End If
End Sub

Private Sub objItems_ItemAdd(ByVal Item As Object)
Call CountUnreadEmails(objInbox, lUnreadItemCount)

lUnreadItemCount = 0
If lUnreadItemCount > 10 Then
MsgBox "Too many unread emails!" & vbCr & "Please deal with them as soon as possible!", vbExclamation + vbOKOnly, "Check Unread Emails"
End If
End Sub

Private Sub CountUnreadEmails(ByVal objFolder As Outlook.Folder, ByRef lCount As Long)
Dim objUnreadItems As Outlook.Items
Dim objSubfolder As Outlook.Folder

Set objUnreadItems = objFolder.Items.Restrict("[Unread] = True")
lCount = objUnreadItems.count + lCount

'Process all subfolders under Inbox recursively
If objFolder.Folders.count > 0 Then
For Each objSubfolder In objFolder.Folders
Call CountUnreadEmails(objSubfolder, lCount)
Next
End If
End Sub


I'm not very familiar with the VB syntax, I've never used it."

To access a shared folder in another user's Exchange server mailbox, you need to use GetSharedDefaultFolder*to reference the mailbox"

How do i add the following code in for shared mail?