The ItemAdd event occurs when an item is added to an Items collection object, either through user action or program code. The following example creates an Items collection object for an Exchange public folder named Customers that contains Contact items. When a new customer is added to the Customers folder, a message is sent to a distribution list for the correct regional sales team. This example requires another event procedure in order to instantiate the collection object for the Customers folder. So that you can better understand the code, each of these event procedures is listed below. See Chapter 11 for a complete listing of the OpenMAPIFolder function.
'Place these declarations in the general section of Class module Public WithEvents colCustomersltems As Outlook.ltems
'Instantiate collection object in objOutlook Startup event
Private Sub objOutlook_Startup() Dim strFolderPath As String
StrFolderPath = "Public Folders\AII Public Folders\"_
& "Contact Management\Customers" Set colCustomersltems = OpenMAPIFolder(strFolderPath).Items End Sub
'This event procedure sends a message to a DL
Private Sub colCustomersltems_ltemAdd(ByVal Item As Object)
Dim objContactltem As Outlook.Contactltem
Dim objMsgltem As Outlook.Mailltem
Set objContactltem = Item
If objContactltem.MessageClass = "IPM.Contact.Customer" Then Select Case objContactltem. BusinessAddressState Case "CA", "NV", "WA", "OR", "AZ", "NM", "ID" strDL = "Sales Team West" Case "IL", "OH", "NE", "MN", "IA", "IN", "Wl" strDL = "Sales Team Midwest"
Case "ME", "NH", "NY", "NJ", "MD", "PA", "Rl", "CT", "MA" strDL = "Sales Team East" Case Else strDL = "Sales Team National" End Select
Set objMsgltem = objOutlook.Createltem(olMailltem) objMsgltem.Subject = "New Customer -" & _ objContactltem. CompanyName objMsgltem.Save objMsgltem.Attachments.Add objContactltem, olByValue 'Must use trusted COM Add-in to prevent OM guard prompts objMsgltem.To = strDL objMsgltem.Send End If
Was this article helpful?