Consulting

Results 1 to 7 of 7

Thread: Application NewMail Event

  1. #1

    Application NewMail Event

    Hi Everyone,

    I currently have code that is running against all new emails I receive under the Application NewMail Event. However it seems that it doesn't work when outlook is first started and a trigger email comes in. It will work on the second and so on, or even after the first email comes in if I manually run it.

    I have tested this a few times with emails that trigger the script, closing outlook and doing it again. Each time, it seems to fail to run on the first attempt but work thereafter.

    Hoping someone has seen this before?

    Thanks!

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Provide the code. If too long, a short example.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    Almost certainly the mail transfer has already begun before the code is loaded. Change your Outlook settings to not do a send and receive on startup.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    I tried changing the send/receive settings and that didn't seem to work upon closing outlook and trying.

    Private Sub Application_NewMail()
    
    
    '______ = New Script
    '****** = Sepeartion in Script
    
    
    'SHARED VARIABLES
        Dim olApp As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Dim objSourceFolder As Outlook.MAPIFolder
            Set olApp = Outlook.Application
            Set objNS = olApp.GetNamespace("MAPI")
            Set objSourceFolder = objNS.GetDefaultFolder(olFolderInbox)
            Set msg = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.GetLast
            'Set msg = Application.ActiveExplorer().selection(1)
        Dim strFilename As String
        Dim LatestFile As String
        Dim LatestDate As Date
        Dim FileDate As Date
    
    
    'VARIABLES FOR PHONE LIST
        Dim newItem As mailItem
        Dim Atmt As Attachment
        Dim fileName As String
            Set newItem = CreateItem(olMailItem)
        
    'VARIABLES FOR JDE REPORT
        Dim olMail As Outlook.mailItem
        Dim olmsg As Outlook.mailItem
        Dim strheader As String              'JDE REPORT 2 ONLY
        Dim Value As String
        Dim MyPath As String
        Dim myShell As Object
            Set Reg1 = New RegExp
            Set myShell = CreateObject("WScript.Shell")
    
    
    
    
    '__________________________________________DETERMINE WHAT EMAIL IT IS_________________________________________________
    
    
    If msg.subject Like "EnterpriseOne*" Then GoTo JDE_Report
       
    If msg.subject = "Employee Phone List - OFFICE PHONE DIRECTORY LIST.doc" Then GoTo Phone_List
    
    
    If msg.subject Like "*Completed Normally" Then
        If msg.Body Like "*Work Center*" Then GoTo Completed_Normally
        'If msg.Sender = "Walther, Joseph" Then GoTo Completed_Normally
       End If
    
    
    Exit Sub
    '______________________________________________________________________________________________________________________
    
    
                                                'OPEN JDE REPORT (ENTERPRISE ONE)
    JDE_Report:
    
    
    'DETERMINE ENVIRONMENT PATH*****************************************
        If (InStr(1, msg.Body, "JDEPRD01", vbTextCompare) > 0) Then
          ' PATH FOR COUNTY & CITY PRODUCTION
          MyPath = "\\JDEPRD01\E900SYS\PRINTQUEUE\"
       Else
            'PATH FOR PY NON-PRODUCTION
          MyPath = "\\JDEDEV01\E900SYS\PRINTQUEUE\"
        End If
    
    
    '********************************************************************
    
    
    'EXTRACTING REPORT # AND VERSION
     
     Value = Replace(msg.subject, " , ", "_")
     Value = Replace(Value, "EnterpriseOne Job ", "")
     Value = Replace(Value, "Completed", "") & "_"
     Value = Replace(Value, " ", "")
    
    
    strFilename = Dir(MyPath & Value & "*.PDF*")
    
    
    '***************************************************************
    
    
    'LOOP THROUGH ALL FILES TO GET JOB # TO COMPLETE PATH
    
    
    Do While Len(strFilename) > 0
           'If the date/time of the current file is greater than the latest
            'recorded date, assign its filename and date/time to variables
        FileDate = FileDateTime(MyPath & strFilename)
            If FileDate > LatestDate Then
                LatestFile = strFilename
                LatestDate = FileDate
            End If
            strFilename = Dir
    Loop
    
    
    '***********************************************************************
    
    
    'Specify the final path to the folder to be executed
    MyPath = MyPath & LatestFile
    
    
    'OPENS THE FILE
    myShell.Run MyPath
    
    
    Exit Sub

  5. #5
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Appears the NewMail event is not appropriate for this purpose. ItemAdd is recommended.

    https://msdn.microsoft.com/EN-US/library/ff869202.aspx

    "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."

    You could try either ItemAdd or NewMailEx described here. Avoid the buggy rules with "run a script" option. http://www.outlookcode.com/article.aspx?id=62
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  6. #6
    Useful thread

  7. #7
    Thanks Skatonni!

Posting Permissions

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