none
Outlook AddIn - Always 1 mailitem left in inbox RRS feed

  • Question

  • Hello Everyone.

    I have an outlook addin I developed thru VS2008. It seems to work pretty good except 1 small detail. When incoming mail comes in I sort it to different folders via the move method (yes, I know I can do with rules but I also do some processing on the item). However, there is always 1 mailitem left. It is always the most recent mailitem. Here is the sub I use to enumerate the inbox folder :

    Private Sub EnumerateMailItems(ByVal oFolder As Outlook.MAPIFolder, ByVal blnUnread As Boolean)
            Dim items As Outlook.Items = Nothing
            Dim strErrorMessage As String = ""
            Dim blnForwardFlag As Boolean = False
    
            Try
            
                'process all items, everytime (testing this)
                items = oFolder.Items
            
                For Each i In items
                    If TypeOf (i) Is Outlook.MailItem Then
                        blnForwardFlag = False
    
                        Dim mailitem As Outlook.MailItem
                        Dim mailitemtoforward As Outlook.MailItem
                        Dim mailitemattachments As Outlook.Attachments
    
                        mailitem = CType(i, Outlook.MailItem)
                        
    
                        If InStr(mailitem.Subject, "555-555-1212", CompareMethod.Text) > 0 And (mailitem.SenderName = "someserver@domain.com") Then
                            ProcessItem(mailitem, strErrorMessage, oFolder) Then
                        ElseIf InStr(mailitem.Subject, "555-555-1213", CompareMethod.Text) > 0 And (mailitem.SenderName = "someserver@domain.com") Then
                            ProcessItem(mailitem, strErrorMessage, oFolder) 
    		    End If
                        If blnForwardFlag Then
                            Try
                                'forward the unprocessed error item with our log file for further examination
                                mailitemtoforward = mailitem.Forward
                                mailitemattachments = mailitemtoforward.Attachments
                                'While mailitemattachments.Count > 0
                                ' mailitemattachments.Remove(1)
                                'End While
                                mailitemtoforward.Subject = "Error from FaxMonitor : " & mailitemtoforward.Subject
                                mailitemtoforward.Recipients.Add("xyzuser@somecompany.com")
                                mailitemtoforward.Attachments.Add("c:\outlookaddin.log")
                                mailitemtoforward.Body = "Error from FaxMonitor. Please check the attached log file to resolve. Error is " & strErrorMessage & "." & mailitemtoforward.Body
                                mailitemtoforward.Send()
                            Catch ex As Exception
                                AppendLog("EnumerateMailItems Forward Error: " & MyErrorMessage(ex).ToString, Const_LogToScreen, Const_LogToFile)
                            End Try
                        End If
                    Else
                        'not a mailitem
                        AppendLog("Not a mailitem.", Const_LogToScreen, Const_LogToFile)
                    End If
                Next
            Catch ex As Exception
                AppendLog("EnumerateMailItems Error: " & MyErrorMessage(ex).ToString, Const_LogToScreen, Const_LogToFile)
            End Try
    
        End Sub

    This sub is called from the Application_NewMail event.

    inboxFolder = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
                    EnumerateMailItems(inboxFolder, True)

    Am I misunderstanding the order of the events? Or Am I missing something else?

    Thanks in advance for any help!

    -ILJester


    Wednesday, February 6, 2013 4:15 PM

Answers

  • If you are moving the message, than you are altering the contents of the collection you are looping through and it won't be accurate when you iterate through other messages in the collections.  You need to loop backwards, like:

    For intX As Integer = Items.Count To 1 Step -1


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    • Marked as answer by ILJester Wednesday, February 6, 2013 7:49 PM
    Wednesday, February 6, 2013 4:35 PM
    Moderator

All replies

  • If you are moving the message, than you are altering the contents of the collection you are looping through and it won't be accurate when you iterate through other messages in the collections.  You need to loop backwards, like:

    For intX As Integer = Items.Count To 1 Step -1


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    • Marked as answer by ILJester Wednesday, February 6, 2013 7:49 PM
    Wednesday, February 6, 2013 4:35 PM
    Moderator
  • Thank you Eric. This worked like a charm. I had not even considered that.

    -ILJester

    Wednesday, February 6, 2013 7:49 PM