2 Attachment(s)
[RESOLVED] Outlook NewEmailEx event won't fire in small VB6 application
I'm needing to run a small VB6 app that will sit in the system tray with the purpose of detecting specific incoming Outlook email messages and depending on the sender, subject and time of day, initiate a custom text page to various recipients spread out around the globe.
The only issue I've encountered is getting the NewEmailEx event to fire when the Outlook client receives a new email message. In addition to using VB6, I am also using Outlook 2010 and have a reference to the Microsoft Outlook 14.0 Object Library.
After spending a week plus searching and reading everything I could find on the subject, nothing I've tried thus far has been a success and the time has come to reach out for some assistance from the pro's (ie; RobDog888, koolsid and the like). If what I need to do as explained above is possible, any further information on the matter would be greatly appreciated.
In a small test project, I've finally got the following code but again, the NewEmailEx event never fires. Please excuse the bad formatting but I couldn't get proper indentions to retain.
Form1.frm
Option Explicit
Public myEmailMonitor As Class1
Private Sub Command1_Click()
Unload Me
End Sub
Public Sub Form_Initialize()
Set myEmailMonitor = New Class1
End Sub
Private Sub Form_Load()
Show
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
End
End Sub
Class1.cls
Option Explicit
Public WithEvents OutlookApplication As Outlook.Application
Sub Initialize_Handler()
Set OutlookApplication = Application
End Sub
Public Sub OutlookApplication_NewEmailEx(ByVal EntryIDCollection As String)
Dim objOutlookNameSpace As Outlook.NameSpace
Dim objOutlookMailApp As MailItem
Dim objOutlookMailItem As Outlook.MailItem
Dim strEntryIDValue() As String
Dim intEntryIDIndex As Integer
On Error Resume Next
Set objOutlookNameSpace = Application.Session
strEntryIDValue = Split(EntryIDCollection, ",")
For intEntryIDIndex = 0 To UBound(strEntryIDValue)
Set objOutlookMailApp = objOutlookNameSpace.GetItemFromID(strEntryIDValue(intEntryIDIndex))
If objOutlookMailApp.Class = olMail Then
Set objOutlookMailItem = objOutlookMailApp
Debug.Print Now & " " & objOutlookMailItem.Sender & " " & _
"(" & objOutlookMailItem.SenderName & ") " & _
"[" & objOutlookMailItem.SenderEmailAddress & "]"
Debug.Print Now & " " & objOutlookMailItem.Subject
Form1.List1.AddItem objOutlookMailItem.Sender
Form1.List2.AddItem objOutlookMailItem.SenderName
Form1.List3.AddItem objOutlookMailItem.SenderEmailAddress
Form1.List4.AddItem objOutlookMailItem.Subject
End If
Next
Set objOutlookNameSpace = Nothing
Set objOutlookMailApp = Nothing
Set objOutlookMailItem = Nothing
End Sub
Re: Outlook NewEmailEx event won't fire in small VB6 application
Could you please wrap your code in [highlight="language here"]code here[/highlight] it will make it easier too read! Also, you should not use "End" to close you program read this to find out why.
Re: Outlook NewEmailEx event won't fire in small VB6 application
First thing I'd do is to remove the 'On Error Resume Next' statement as it may be ignoring a significant error.
Next thing I'd do is to change the Initialize_Handler to read
Code:
Set OutlookApplication = New Outlook.Application
and see what happens.
EDIT: On reflection, I'd remove the Initialise_Handler sub routine altogether (as I don't think it'll ever be executed by VB6 - it might work in VBA - I don't know) and replace it with
Code:
Private Sub Class_Initialize()
Set OutlookApplication = New Outlook.Application
End Sub
Also I'd get rid of the Form_Initialise event altogether, and modify the Form_Load to:
Code:
Private Sub Form_Load
Set myEmailMonitor = New Class1
End Sub
You could also have a 'terminate' sub routine to shutdown Outlook and tidy up
Code:
Private Sub Terminate
Set OutlookApplication = Nothing
Set MyEmailMonitor = Nothing
End Sub
change Command1_Click to
Code:
Private Sub Command1_Click
Terminate
Unload me
End Sub
and the Query_Unload event to
Code:
Private Sub Query_Unload
Terminate
End Sub
Re: Outlook NewEmailEx event won't fire in small VB6 application
Quote:
Originally Posted by
Nightwalker83
Could you please wrap your code in [highlight="language here"]code here[/highlight] it will make it easier too read! Also, you should not use "End" to close you program read
this to find out why.
Thanks for the tip Nightwalker83. :o
Re: Outlook NewEmailEx event won't fire in small VB6 application
I appreciate the suggestions, Doogle. Unfortunately, they did not make a difference as the NewEmailEx event is still not getting triggered.
Re: Outlook NewEmailEx event won't fire in small VB6 application
Quote:
Originally Posted by
ljwheeler
After spending a week plus searching and reading everything I could find on the subject, nothing I've tried thus far has been a success
Oops
I think I've found the problem. The event is named: 'NewMailEx' not 'NewEmailEx' :)
Code:
Public Sub OutlookApplication_NewMailEx(ByVal EntryIDCollection As String)
Works for me using Outlook 2003
Re: Outlook NewEmailEx event won't fire in small VB6 application
Quote:
Originally Posted by
Doogle
Oops
I think I've found the problem. The event is named: 'NewMailEx' not 'NewEmailEx' :)
Code:
Public Sub OutlookApplication_NewMailEx(ByVal EntryIDCollection As String)
Works for me using Outlook 2003
You are a scholar and a gentleman!!! What a boneheaded oversight on my part. I sincerely appreciate you taking your valuable time to resolve this.
Re: [RESOLVED] Outlook NewEmailEx event won't fire in small VB6 application
I think we've all been there and done that
Good luck