Processing a list of emails

L

Leon Mayne

Hi all,
I have a bit of code that loops through the emails the user has selected,
processing them and deleting them at the end, but it seems to be having
problems all of a sudden. After it processes the first email, it crashes
when I try to get the next email's entryid:

For x = 1 To oOL.ActiveExplorer.Selection.Count
strID = oOL.ActiveExplorer.Selection.Item(x).EntryID
strStoreID = oOL.ActiveExplorer.Selection.Item(x).Parent.StoreID
-- Do stuff --
oOL.ActiveExplorer.Selection.Item(x).Delete
Next

It crashes on the strID line. It seems to be related to the delete line,
because if I comment out the delete line it works OK. It also seems to be
time dependent, because if I click 'debug' on the error message and then
click 'start' it carries on OK (but crashes on the next loop etc). The error
message just says "Run time error '-1767636721 (96a4010f)': The operation
failed".

Can anyone help?
 
K

Ken Slovak - [MVP - Outlook]

Use a down counting loop when you delete items from a collection.
Otherwise the loop counter is being messed with in your loop code.

For x = oOL.ActiveExplorer.Selection.Count To 1 Step -1
 
L

Leon Mayne

Ken said:
Use a down counting loop when you delete items from a collection.
Otherwise the loop counter is being messed with in your loop code.

For x = oOL.ActiveExplorer.Selection.Count To 1 Step -1

Thanks for the suggestion, but I'm afraid it still produces the same error.

I think your idea is along the right lines, because it does not happen in
any of my imap accounts (where the message is marked for deletion but not
removed from the message list) but it does happen in my exchange account.

Is there any way to 'refresh' the folder list after deleting an email?
 
K

Ken Slovak - [MVP - Outlook]

What do you mean by refreshing the folder list? The Folder List
displays folders, not items.

I've used similar code to delete items from a collection like
Selection with no errors. You can try getting the Selection.Count
property as a Long and using that in the For loop and see if that
helps.
 
L

Leon Mayne

Ken said:
What do you mean by refreshing the folder list? The Folder List
displays folders, not items.

Sorry, I meant the message list. I presume the problem is that it is trying
to access the list of selected items as one of them is being deleted, so I
thought that if I refreshed the list before the next loop then it might
work.
I've used similar code to delete items from a collection like
Selection with no errors. You can try getting the Selection.Count
property as a Long and using that in the For loop and see if that
helps.

Tried that, same error.

I suppose I could either:

1) Delete the items after the process loop, when all of the selected items
have been deleted, or
2) Just put a pause of about half a second in after each item has been
deleted.

The second one is a bit scrappy though. Would the first one work, or would
it just have the same problem as the original problem?
 
L

Leon Mayne

Leon said:
1) Delete the items after the process loop, when all of the selected
items have been deleted, or

Sorry, that should've been "After each item has been processed"!
 
K

Ken Slovak - [MVP - Outlook]

Maybe try inserting a DoEvents statement in the loop and see if that
helps.
 
L

Leon Mayne

Ken said:
Maybe try inserting a DoEvents statement in the loop and see if that
helps.

Aha! That worked. I just put a DoEvents statement in after the delete line.

Thanks very much :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top