none
Using For i = olSourceFolder.Items.Count To 1 Step -1?? RRS feed

  • Question

  • Thanks to many of you, I have developed a VB.net program to read and parse Outlook 2016 Mail Items, move them to a completed folder and create a  tab delimited file for each item to be picked up and used by another program.  Considering I knew nothing about programing or vb.net I'm pretty happy with it BUT...

    I'm using a For Next statement to read each mail item in the Inbox.  My concern is that new emails are constantly coming in which would "seem" to throw off the 'i' value.  Yet in all my testing, by adding new mail item to the inbox after the value of 'i' is set, all the items, even the added ones get processed.  (I add new mail items by placing a break in the code and adding the items while program in paused.)  Maybe I'm worried about nothing but this just does not seem right and I don't want the program to blow up once in production.  Any comments or suggestions??

    Here is a bit of the code:

    For i = olSourceFolder.Items.Count To 1 Step -1
                Try
                    mi = TryCast(mi, Outlook.MailItem)
                    mi = olSourceFolder.Items(i)

                    'Parse out the eCommerse Cart Order Number
                    subject = mi.Subject
                   emailBody = mi.Body

                    'Parse each Email Line
                    Dim emailBodyLines As String() = emailBody.Split(separators, StringSplitOptions.RemoveEmptyEntries) ' an array to hold each 'line' of the email

                    ……...do more parsing

    Next    '  Get next email              


    Carl

    Sunday, October 27, 2019 8:30 PM

All replies

  • Hi

    There might well be other considerations.

    1. Do you process these emails time after time - ie every time this procedure is run?Or, do you keep track of processed files and skip them next time?

    2. Do the new incoming files need to be processed also?

    *

    I am thinking along the lines of a one time batch process followed by a FileWatcher to process new files.


    Regards Les, Livingston, Scotland

    Sunday, October 27, 2019 9:13 PM
  • Maybe I'm worried about nothing.

    Yeah well take that to heart, becuase as long as the array collection Count() increases due to  a new item is being added to the array or collection, I don't see a problem. The loop goes to the max Count() incrementing 'i' until max Count() is reached.

    What is set at any given point is the Count() as "i" the indexer to access an item in the array or collection is about accessing an item in the array or collection by the indexer,  and 'i' is a fluent number with 'l' increasing by 1 until 'i' is greater than the Count(), which in the max number of items in the array or collection at any given time.

    Count() is a function that always accesses the array or collection count,  and the loop is executing Count() in the loop execution, becuase otherwise, you wouldn't be accessing new items added to the array or collection while the loop is executing. 

    Sunday, October 27, 2019 9:27 PM
  • Yea many people think that processing takes for human understandable time. 

    It is not, it goes many times faster, moreover, if you don't use async in this. The process is done until the end of the method (sub or function). Then comes a new step to handle the events. 


    Success
    Cor

    Sunday, October 27, 2019 9:35 PM
  • 1. The routine keeps running listening for new email.  It constantly runs.

    2. All new emails get processed and moved. (Some are not parsed depending on content.)


    Carl

    Sunday, October 27, 2019 11:47 PM
  • Hi,

    Maybe what you are worried about is right, but from my point of view, I agree with the DA924x and I don't see anything wrong with it.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 28, 2019 8:21 AM
    Moderator
  • I think that if new items are added to any place of the collection during the loop, then some of the items will not be processed. But if your task runs periodically, then the skipped items will be processed on some next iteration.

    Monday, October 28, 2019 8:31 AM
  • Use a for each loop instead of a counter.  That way you don't care about the count.
    Monday, October 28, 2019 2:02 PM