Results 1 to 8 of 8

Thread: [RESOLVED] Outlook NewEmailEx event won't fire in small VB6 application

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    NE, OK (USA)
    Posts
    10

    Resolved [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
    Attached Files Attached Files

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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
    Last edited by Doogle; Apr 6th, 2013 at 05:43 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    NE, OK (USA)
    Posts
    10

    Re: Outlook NewEmailEx event won't fire in small VB6 application

    Quote Originally Posted by Nightwalker83 View Post
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    NE, OK (USA)
    Posts
    10

    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.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Outlook NewEmailEx event won't fire in small VB6 application

    Quote Originally Posted by ljwheeler View Post
    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
    Last edited by Doogle; Apr 6th, 2013 at 11:18 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    NE, OK (USA)
    Posts
    10

    Re: Outlook NewEmailEx event won't fire in small VB6 application

    Quote Originally Posted by Doogle View Post
    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.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] Outlook NewEmailEx event won't fire in small VB6 application

    I think we've all been there and done that

    Good luck

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width