Reputation: 1
I was wondering if you guys can help me out. We recently moved from Outlook 2003 to 2010. We use an Outlook VBA script to take specific incoming email forms, read through them, extract out the information and compile it into a file.
The script works, but it only reads half of the messages in the folder. I did a count test on the folders, and that shows the correct number, but for some reason, the for loop i am using only executes on half of the items. So if there are 8 messages in the folder, then it only reads 4 of them, if there are 4 then it only reads 2, etc...
I cannot figure out what portion of my code is bombing. Any help would be greatly appreciated. I've posted only the for loop portion of my code. if you need the whole whole script, please let me know.
enter code here Set myOlApp = CreateObject("Outlook.Application") 'Outlook App Obj.
Set myNameSpace = myOlApp.GetNamespace("MAPI") 'MAPI Namespace
Set myFolder = myNameSpace.Folders("myemail@mydomain").Folders("TestAccMail") 'Outlook folder to access
For Each Item In myFolder.Items 'Loop through each mail item
If (regex.Test(Item.Subject)) Then 'Test for TestAccX Message
strDataSplit = Split(Item.Body, vbNewLine) 'Split the contents of the body to an array
strOutput = ""
For Each arrItem In strDataSplit 'Loop through the contents of the e-mail body
If (regExData.Test(arrItem)) Then 'Test if line contains a field we need
field = Split(arrItem, ":")(1) 'Store the value of the field
strOutput = strOutput & Trim(Replace(field, Chr(160), "")) & "|" 'Concat the previous field value with current; seperated by |
End If
Next arrItem 'Next field in array
If Not strOutput = "" Then 'Ensure the output var has data
WriteToATextFile strOutput, file 'Append the data record to the provided file
Item.Move myFolder.Folders("TestAcc Complete") 'Move mail item to completed folder
recCount = recCount + 1
Else 'If the string is blank, no data was extracted; Error!
Item.Move myFolder.Folders("Errors") 'Move mail item to Error folder
errCount = errCount + 1 'Incremeant error count
End If
messCount = messCount + 1 'Incremeant message count, error or not
End If
Next 'Next TestAccX Message
Upvotes: 0
Views: 833
Reputation: 5121
I am not sure how VBA handles collections but I'm guessing when you are moving items from the myFolder you are actually "hopping over" in the collection. Proper languages would not allow you to alter collection which is being processed by for each loop.
Upvotes: 1