Msgbox prompt on Delegate Inbox

  • Thread starter Thread starter Sydney
  • Start date Start date
S

Sydney

Hi all

Using Outlook 2003 vba, I am trying to intercept when new mail arrives on an
additional mailbox. I have found code that does exactly what I need however
it only works on the default mailbox. I tweaked the code from
http://www.vbforums.com/archive/index.php/t-251607.html hoping it will work
on a delegate Inbox, however it does not run automatically.

Am I wasting my time? Is this possible? ... Thanks in advance.

MY CODE:

''**WORKS WHEN RUN MANUALLY**
Private Sub Application_NewMail()

Dim oInbox As Outlook.MAPIFolder
Dim oEmail As Outlook.MailItem
Dim myNamespace As Outlook.NameSpace

Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Barrier Officer")
myRecipient.Resolve
If myRecipient.Resolved Then

Set oInbox = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderInbox)

If oInbox.UnReadItemCount > 0 Then MsgBox "New Email!", vbOKOnly +
vbInformation

Set oInbox = Nothing
Set oNS = Nothing
End If

End Sub
 
You would need to use a different event -- MAPIFolder.Items.ItemAdd -- on the other mailbox's Inbox. See http://www.outlookcode.com/d/code/zaphtml.htm for an example of that event handler that you should be able to adapt.

Also note that you should never use this statement in Outlook VBA code:

Set myOlApp = CreateObject("Outlook.Application")

Instead, derive all Outlook objects from the intrinsic Application object.
 
Thanks for replying. I am just not sure what lines to put in and where.
Ive tried a few scenarios that have failed.

Would you mind telling what lines I need to include?


You would need to use a different event -- MAPIFolder.Items.ItemAdd -- on
the other mailbox's Inbox. See http://www.outlookcode.com/d/code/zaphtml.htm
for an example of that event handler that you should be able to adapt.

Also note that you should never use this statement in Outlook VBA code:

Set myOlApp = CreateObject("Outlook.Application")

Instead, derive all Outlook objects from the intrinsic Application object.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"Sydney" wrote in message news:[email protected]...
 
Did you look at the example I suggested? It -- and the Help topic on ItemAdd show how to use it. If there's something you don't understand about the examples, let's talk about it.
 
Yes, I have tried a few versions with your sample and the help topic. I
guess I just dont know what I am doing by trying to call an additional
mailbox.

I haven't given up yet though... thanks for your help :))

This is my latest version created in a Class Module:

Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
Dim myRecipient As Outlook.Recipient
Dim myNamespace As Outlook.NameSpace

Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Information Barrier Officer")

'Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
Set myOlItems =
myOlApp.GetNamespace("MAPI").GetSharedDefaultFolder(myRecipient,
olFolderInbox).Items


End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim myOlAtts As Outlook.Attachments
Set myOlMItem = myOlApp.CreateItem(olMailItem)

If myOlItems.UnReadItemCount > 0 Then MsgBox "New Email!", vbOKOnly +
vbInformation
End Sub



Did you look at the example I suggested? It -- and the Help topic on ItemAdd
show how to use it. If there's something you don't understand about the
examples, let's talk about it.
 
Hi Sue

I guess my major issue is that I cannot adapt my code to work on an
additional mailbox. I have tried a few code sceniarios... all work on my
default inbox.

The latest code from your sample also works perfectly on the default inbox,
when I try to call the additional mailbox it fails. Here it is and I
haven't given up yet.. any direction would be highly appreciated :)

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Dim myRecipient As Outlook.Recipient

Set objNS = Application.GetNamespace("MAPI")

Set myRecipient = objNS.CreateRecipient("Barrier Officer") 'added
myRecipient.Resolve 'added

' instantiate objects declared WithEvents
' Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set olInboxItems = objNS.GetSharedDefaultFolder(myRecipient,
olFolderInbox).Items

Set objNS = Nothing
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next

If Item.UnRead Then
MsgBox "You have an unread item in Barrier Inbox"
End If

Set Item = Nothing
End Sub



Did you look at the example I suggested? It -- and the Help topic on ItemAdd
show how to use it. If there's something you don't understand about the
examples, let's talk about it.
 
I don't see any problem with the code below. What in particular fails? Does myRecipient resolve? Does objNS.GetSharedDefaultFolder(myRecipient, olFolderInbox) return a valid MAPIFolder object?
 
Yayyy... This is now working.
I had an empty Class module, after removing this module and restarting
Outlook it works perfectly... didnt think an empty module would have any
effect but there you go.

Thank you so much for your help


I don't see any problem with the code below. What in particular fails? Does
myRecipient resolve? Does objNS.GetSharedDefaultFolder(myRecipient,
olFolderInbox) return a valid MAPIFolder object?
 
Back
Top