ijourneaux
06-05-2023, 05:22 AM
I have created a inboxItems_ItemAdd macro that on receiving an email it moves the email to a different folder based on email subject (creating the folder if needed). This has worked well but I noticed that sometimes the new emails may not get processed. This could be related to me not being logged in on my computer when message arrives but I haven't identified the precise circumstances when it happens.
Any thoughts as to what might be happening and how I could handle it so messages are always handled correctly?
Appreciate any comments.
Ian
Aussiebear
06-05-2023, 01:15 PM
Welcome to VBAX ijourneaux. May we see your macro, in case this jogs someone's knowledge.
ijourneaux
06-05-2023, 01:59 PM
I am not trying to do anything too sophisticated. I am thinking that this might be related to which device sees the new email first. This code exists on my windows PC and seems to work work. Perhaps the issue occurs if I first see the new message on my phone when my windows PC is offline.
Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim Subject As String
Dim subjectREFW As String
Dim Company As String
Dim Mill As String
Dim Process As String
Dim ReplyorForward As Boolean
Dim MessageType As String
Dim folderobj As Folder
Dim TempItemCopy As Outlook.MailItem
Dim SubFolderObj As Folder
Dim foldername As String
Dim ns As Outlook.NameSpace
Dim bExceptionReport As Boolean
Dim bShiftReport As Boolean
Dim val As Integer
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
Dim MessageInfo
Dim Result
On Error GoTo ErrorHandler
If TypeName(Item) = "MailItem" Then
Subject = Item.Subject
If (InStr(1, Subject, "Exceptions Report for", vbTextCompare) > 0) Then
bExceptionReport = True
Else
bExceptionReport = False
End If
If (InStr(1, Subject, "Shift Reports for", vbTextCompare) > 0) Then
bShiftReport = True
Else
bShiftReport = False
End If
If (bExceptionReport) Then
val = InStr(1, Subject, "Exceptions Report for", vbTextCompare)
Mill = Mid(Subject, val + Len("Exceptions Report for"))
Mill = Trim(Left(Mill, InStr(1, Mill, "(", vbTextCompare) - 2))
Company = (Left(Mill, InStr(1, Mill, " ", vbTextCompare) - 1))
Mill = Trim(Right(Mill, Len(Mill) - (InStr(1, Mill, Company, vbTextCompare) + Len(Company))))
Company = Trim(Company)
Mill = Trim(Mill)
If (InStr(1, Left(Subject, 3), "FW:", vbTextCompare) Or (InStr(1, Left(Subject, 3), "RE:", vbTextCompare))) Then
ReplyorForward = True
Subject = Trim(Right(Subject, Len(Subject) - 3))
Else
ReplyorForward = False
End If
Process = Trim(Left(Subject, InStr(1, Subject, "Exceptions Report for", vbTextCompare) - 1))
Set CompanyFolderObj = CreateFolderIfNeeded(Company, inboxFolders)
Set SiteFolderObj = CreateFolderIfNeeded(Mill, CompanyFolderObj)
Set ProcessFolderObj = CreateFolderIfNeeded(Process, SiteFolderObj)
Set ExceptionReportsFolderObj = CreateFolderIfNeeded("Exception Reports", ProcessFolderObj)
Set ShiftReportsFolderObj = CreateFolderIfNeeded("Shift Reports", ProcessFolderObj)
If (ReplyorForward) Then
' Set TempItemCopy = Item.Copy
' TempItemCopy.Move ProcessFolderObj.Folders("Exception Reports")
' Set TempItemCopy = nothing
Else
Item.Move ProcessFolderObj.Folders("Exception Reports")
End If
End If
If (bShiftReport) Then
val = InStr(1, Subject, "Shift Reports for", vbTextCompare)
openPos = InStr(1, Subject, "-", vbTextCompare)
closePos = InStr(openPos + 1, Subject, "-")
Mill = Trim(Mid(Subject, openPos + 1, closePos - openPos - 1))
Company = Trim(Mid(Subject, val + Len("Shift Reports for"), openPos - (val + Len("Shift Reports for"))))
If (InStr(1, Left(Subject, 3), "FW:", vbTextCompare) Or (InStr(1, Left(Subject, 3), "RE:", vbTextCompare))) Then
ReplyorForward = True
Else
ReplyorForward = False
End If
openPos = InStr(1, Subject, "-", vbTextCompare)
closePos = InStr(openPos + 1, Subject, "-")
Process = Trim(Mid(Subject, closePos + 1, InStr(closePos, Subject, "KPIs", vbTextCompare) - closePos - 1))
Set CompanyFolderObj = CreateFolderIfNeeded(Company, inboxFolders)
Set SiteFolderObj = CreateFolderIfNeeded(Mill, CompanyFolderObj)
Set ProcessFolderObj = CreateFolderIfNeeded(Process, SiteFolderObj)
Set ExceptionReportsFolderObj = CreateFolderIfNeeded("Exception Reports", ProcessFolderObj)
Set ShiftReportsFolderObj = CreateFolderIfNeeded("Shift Reports", ProcessFolderObj)
If (ReplyorForward) Then
' Set TempItemCopy = Item.Copy
' TempItemCopy.Move ProcessFolderObj.Folders("Shift Reports")
' Set TempItemCopy = nothing
Else
Item.Move ProcessFolderObj.Folders("Shift Reports")
End If
End If
Set CompanyFolderObj = Nothing
Set SiteFolderObj = Nothing
Set ProcessFolderObj = Nothing
Set ExceptionReportsFolderObj = Nothing
Set ShiftReportsFolderObj = Nothing
End If
ExitNewItem:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description 'enter code here`
Resume ExitNewItem
End Sub
struttelling
09-21-2023, 09:13 PM
I have created a inboxItems_ItemAdd macro that on receiving an email it moves the email to a different folder based on email subject (creating the folder if needed). This has worked well but I noticed that sometimes the new emails may not get processed. This could be related to me not being logged in on my computer when message arrives but I haven't identified the precise circumstances when it happens.
Any thoughts as to what might be happening and how I could handle it so messages are always handled correctly?
Appreciate any comments.
Ian
mini crossword (https://minicrossword.io/)
Actually, sometimes we often don't pay too much attention to the source code. While using it I rarely learn about it. Thank you for sharing this issue. Really useful information
jdelano
09-24-2023, 02:32 AM
So, this macro is triggered when a new email is received. I'd have to agree that if you view the email on your phone, thus making it no longer new, the macro will not be triggered in Outlook. I would investigate if it is possible to move this to the email server instead, so that regardless of the client you view the emails from the email has been processed. I've been poking around some documentation and so far, do not see where it is possible to run code or create a folder via a rule on outlook.com (if this is the email server you're using). If you're using another server (gmail for instance) maybe they have more flexible rule functionality.
tonyadams
10-18-2023, 06:21 PM
I suggest adding a short delay before the macro processes incoming emails which allows time for the email client to fully receive and save the new email, reducing the risk of processing incomplete data. contexto (https://contexto-wordle.com/)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.