WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE COM ADO CDO ADSI LDAP
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ForEach Problem


Question:

I had a little problem by walking through all inbox messages and deleting them using a foreach next loop. Not all items were deleted afterwards.

I'm using a workaround now so for me it's not a problem anymore but I wonder if this is caused by a bug or the fact that I misunderstand it's working.

In the construction below there is still a message left in the inbox after execution of the code.

By deleting the items during the loop the collection is changed. Don't know how this is implemented but can imagine that this behavior is caused by a bug. or am I doing something wrong here?


PR_SENDER_EMAIL_ADDRESS= 203358238
o_r_Item = CreateObject("Redemption.SafeMailItem") 
oSession = CreateObject("MAPI.Session")
oSession.Logon

__________ code which does not delete all inbox messages __________
ofolder= oSession.inbox
foreach oMessage in oFolder.messages 
o_r_Item.Item= oMessage 
Address= o_r_Item.Fields( PR_SENDER_EMAIL_ADDRESS )
o_r_Item.delete
next 

__________ succesfull work around ________
ofolder= oSession.inbox
foreach oMessage in oFolder.messages 
o_r_Item.Item= oMessage 
Address= o_r_Item.Fields( PR_SENDER_EMAIL_ADDRESS )
next 

while ( oFolder.messages.count > 0 ) 
o_r_Item.Item = oFolder.Messages.item(1)
o_r_Item.delete
endwhile
_________________

Answer:

From the WB help file under the ForEach topic:

"It is also recommended that you not alter the collection by adding, deleting, replacing, or reordering any elements. If you alter the collection after you have initiated a For Each...Next loop, the Collection object becomes invalid, and the next attempt to access an element may result in an error message or other unexpected behavior."


Article ID:   W16554
File Created: 2005:02:18:12:21:26
Last Updated: 2005:02:18:12:21:26