How to trigger ItemAdd, from a class module, when I receive a message?
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
D开发者_如何转开发im 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
Instead of trying to have the routine initialize in ThisOutlookSession, Manage Rules & Alerts to Run a Script, where your script will be your routine. -Redplaya
Just a sanity check here... have you created an instance of this class?
i.e. something like:
Dim c As MyClass
Private Sub Application_Startup()
c = New MyClass
' If you don't rename Initialize_handler, you'll need:
' c.Initialize_handler
End Sub
in ThisOutlookSession...
I expect you'll also be wanting to change Initialize_handler
to Class_Initialize
unless you want to have to make an explicit call to it...
精彩评论