• New Mail Event fails to trigger code (Office 2007)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » New Mail Event fails to trigger code (Office 2007)

    Author
    Topic
    #452032

    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(olFolderInbox)
    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

    Viewing 0 reply threads
    Author
    Replies
    • #1114673

      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(olFolderInbox)
      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

      • #1114675

        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(olFolderInbox)
        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

        • #1114683

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

        • #1114751

          >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[/url%5D, or perhaps from Rory, 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.

    Viewing 0 reply threads
    Reply To: New Mail Event fails to trigger code (Office 2007)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: