Consulting

Results 1 to 6 of 6

Thread: Issue with inboxItems_ItemAdd

  1. #1

    Issue with inboxItems_ItemAdd

    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

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,050
    Location
    Welcome to VBAX ijourneaux. May we see your macro, in case this jogs someone's knowledge.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    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

  4. #4
    Quote Originally Posted by ijourneaux View Post
    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
    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

  5. #5
    VBAX Regular
    Joined
    Sep 2023
    Posts
    97
    Location
    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.

  6. #6
    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

Posting Permissions

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