Question

I have a code that triggers the subroutine Items_ItemAdd when emails come in. It works perfectly for my own personal email inbox. Here is the code I use, it's written in the default Outlook module called ThisOutlookSession:

Option Explicit

Public WithEvents Items As Outlook.Items

Public Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Public Sub Items_ItemAdd(ByVal Item As Object)
  On Error GoTo ErrorHandler
MsgBox "Replace the code for this message with your subroutine"
ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

The problem is that I have tried countless ways to make this work for a shared email inbox which my boss just added me into the group. F.y.i. after he had added me to the list of users of this shared email I had to add it by File->Info->Account Settings->Account Settings->Change->More Settings->Advanced->add and there I had to write the name of the shared email-After trying all kinds of solutions to make the subroutine work for the shared email, e.g.

http://social.msdn.microsoft.com/Forums/office/en-US/b85f08f0-4f6b-4663-a75e-272350c07d2c/vba-outlook-2010-how-to-detecte-the-new-email-in-shared-mailbox?forum=outlookdev

http://www.slipstick.com/developer/code-samples/process-items-shared-mailbox/

http://www.outlookcode.com/article.aspx?id=62

non of it was helpful for me because when I implement it don't get any error messages, it's like it's impossible for me to have an event triggered for this shared email inbox. My most hopeful attempt is where I figured out by using the vba debugger watch how to locate the Items for the shared email inbox and I replaced the line below:

Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

with a new line to point to the Items of the shared email inbox:

Set Items = objNS.Folders.Item(4).Items.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items

To me it seems to be the right Items so I thought this would defiantly work, but the same story, nothing happens. The problem is, I have a code that works perfectly for my email's inbox, why can't I make it work for a shared email's inbox?

Was it helpful?

Solution

What I don't see is any calls to NameSpace.GetSharedDefaultFolder. If you pass the Inbox owner's email address as an argument to that method, you will then get the proper Folder object for that shared folder. Then you can get Folder.Items and thus access the Items.ItemAdd event.

NameSpace.GetSharedDefaultFolder Method (Outlook) http://msdn.microsoft.com/en-us/library/office/ff869575(v=office.15).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top