PDA

View Full Version : Application NewMail Event



colonials13
09-28-2015, 08:42 AM
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!

skatonni
09-28-2015, 01:48 PM
Provide the code. If too long, a short example.

gmayor
09-28-2015, 08:18 PM
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.

colonials13
09-29-2015, 07:13 AM
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

skatonni
09-30-2015, 12:05 PM
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 (https://msdn.microsoft.com/EN-US/library/ff869609.aspx) 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

methewandrew
10-05-2015, 04:47 AM
Useful thread

colonials13
10-05-2015, 05:16 AM
Thanks Skatonni!