Why doesn't Item.ItemAdd event work?

T

typingcat

The following is a very simple VBScript to popup a massage box when new
mails arrived to the inbox.
========================================================
Dim Inbox As Folder
Dim InboxMails As Items

Private Sub Application_Startup()
Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set InboxMails = Inbox.Items
End Sub

Private Sub InboxMails_ItemAdd(ByVal Item As Object)
MsgBox "Item ADD"
End Sub
Private Sub InboxMails_ItemChange(Item As Object)
MsgBox "item change"
End Sub
========================================================
I also added this to check whether InboxMails is properly set;

Private Sub Application_FolderContextMenuDisplay(ByVal CommandBar As
Office.CommandBar, ByVal Folder As MAPIFolder)
'To test InboxMails is properly set.
Set Inbox = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set InboxMails = Inbox.Items
MsgBox "There " & InboxMails.Count & " mails in the inbox"
End Sub

When I right cliked a folder the massage box worked as I expected. Thus
I'm sure
that the InboxMails is set to an Items object. But InboxMails_ItemAdd
nor
InboxMails_ItemChange worked. I tried many times but in vein. What's
wrong
with my code? Please help me. Thank you.
 
S

Sue Mosher [MVP-Outlook]

The one known issue is that ItemAdd won't fire if more than 16 items are added to the folder at one time.

How about ItemChange? Is that firing for the Inbox folder? If you manually run the Application_Startup procedure, do both Items event handlers start working?

FWIW, that's not VBScript, but VBA code. There's a big difference.
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
T

typingcat

I'm sorry. I think I wrote the code wrong .I replaced Dim InboxMails As
Items with Public WithEvents InboxMails As Outlook.Items, and it worked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top