I researched the ItemAdd event and created a class module that follows the example. My macro won't fire when I receive new messages.
The macro parses data within the email and saves it to an Excel file. It is in it's own module. I can run it manually.
I am trying to have it append new data each time I receive a new copy of the specific message.
I have a rule that forwards these emails into a special folder, Folder X, where I want the code to search for new messages.
My code in a class module.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
' Reference the items in the Inbox. Because myOlItems is declared
' "WithEvents" the ItemAdd event will fire below.
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Folder X").Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim count As Integer
Dim myTitlePos As Integer
Dim myTitleLen As Integer
Dim myVarPos As Integer
Dim myVarLen As Integer
Dim strPrice As String
Dim strYear As String
Dim myVarCRLF As Integer
Dim myDate As Date
Dim newLineTest As String
' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then
'This is where all the working data parsing takes place.
End If
End Sub