Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New Mail Event fails to trigger code (Office 2007)

    I've never been able to get the New Mail event to trigger code in any version of Outlook, even though I think I am carrying out all the steps correctly. Office 2007 has this sample which it says to paste into a Class Module; I did this and it STILL DOESN'T WORK even though it is Microsoft's own example. How can this be?
    Public WithEvents myOlApp As Outlook.Application

    Sub Initialize_handler()
    Set myOlApp = Outlook.Application
    End Sub

    Private Sub myOlApp_NewMail()
    Dim myExplorers As Outlook.Explorers
    Dim myFolder As Outlook.Folder
    Dim x As Integer
    Set myExplorers = myOlApp.Explorers
    Set myFolder = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox)
    If myExplorers.Count <> 0 Then
    For x = 1 To myExplorers.Count
    On Error GoTo skipif
    If myExplorers.Item(x).CurrentFolder.Name = "Inbox" Then
    myExplorers.Item(x).Display
    myExplorers.Item(x).Activate
    Exit Sub
    End If
    skipif:
    Next x
    End If
    On Error GoTo 0
    myFolder.Display
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: New Mail Event fails to trigger code (Office 2007)

    The code from the example is intended to be used if you automate Outlook from another application. If you want it to run within Outlook itself, the code can be slightly simpler, and it should be placed in the ThisOutlookSession module:

    Private Sub Application_NewMail()
    Dim myExplorers As Explorers
    Dim myFolder As MAPIFolder ' not Folder!
    Dim x As Integer
    Set myExplorers = Explorers
    Set myFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbo x)
    If myExplorers.Count <> 0 Then
    For x = 1 To myExplorers.Count
    On Error GoTo skipif
    If myExplorers.Item(x).CurrentFolder.Name = "Inbox" Then
    myExplorers.Item(x).Display
    myExplorers.Item(x).Activate
    Exit Sub
    End If
    skipif:
    Next x
    End If
    On Error GoTo 0
    myFolder.Display
    End Sub

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: New Mail Event fails to trigger code (Office 2007)

    Thank you Hans!
    Now you mention it, I did think it was strange to have to use the Initialize Handler events etc when Outlook is already open and going.
    But there is NOTHING in the Microsoft help page on this subject that even vaguely mentions the word Automation - though they DO mention it in most other articles.
    Heavens!!
    Here's their article - unless I'm blind, there's nothing there:
    Outlook Developer Reference
    Application.NewMail Event
    Occurs when one or more new e-mail messages are received in the Inbox. expression.NewMail
    expression A variable that represents an Application object.

    Remarks


    This event is not available in Microsoft Visual Basic Scripting Edition (VBScript).

    The NewMail event fires when new messages arrive in the Inbox and before client rule processing occurs. If you want to process items that arrive in the Inbox, consider using the ItemAdd event on the collection of items in the Inbox. The ItemAdd event passes a reference to each item that is added to a folder.

    The NewMail event does not fire when the user is in offline mode.


    Example


    This Microsoft Visual Basic/Visual Basic for Applications (VBA) example displays the Inbox folder when a new e-mail message arrives. The sample code must be placed in a class module, and the Initialize_handler routine must be called before the event procedure can be called by Microsoft Outlook.

    Visual Basic for Applications
    Public WithEvents myOlApp As Outlook.Application

    Sub Initialize_handler()
    Set myOlApp = Outlook.Application
    End Sub

    Private Sub myOlApp_NewMail()
    Dim myExplorers As Outlook.Explorers
    Dim myFolder As Outlook.Folder
    Dim x As Integer
    Set myExplorers = myOlApp.Explorers
    Set myFolder = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox)
    If myExplorers.Count <> 0 Then
    For x = 1 To myExplorers.Count
    On Error GoTo skipif
    If myExplorers.Item(x).CurrentFolder.Name = "Inbox" Then
    myExplorers.Item(x).Display
    myExplorers.Item(x).Activate
    Exit Sub
    End If
    skipif:
    Next x
    End If
    On Error GoTo 0
    myFolder.Display
    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: New Mail Event fails to trigger code (Office 2007)

    The help files for Office applications and their VBA object models leave a lot to be desired.

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: New Mail Event fails to trigger code (Office 2007)

    >If you want to process items that arrive in the Inbox, consider using the ItemAdd event on the collection of items in the Inbox.
    >The ItemAdd event passes a reference to each item that is added to a folder.


    I can't find evidence right now, perhaps it was on http://www.outlookcode.com, or perhaps from <!profile=Rory>Rory<!/profile>, but I have heard that the NewMail Event is not reliable if there's a lot of incoming Mail volume, where Item_Add reliably operates on every new Item added to the target folder.

    You may also be interested to know that the "ThisOutlookSession" Module is a sort of Class Module, where certain Events can be handled. For a very simple example, see <post:=714,194>post 714,194</post:>.
    -John ... I float in liquid gardens
    UTC -7ąDS

Posting Permissions

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