Delete Message with VBA

  • Thread starter Thread starter Luis
  • Start date Start date
L

Luis

Can someone let me know how to delete four messages in a Folder? I have tried
to find information on it but there is not VBA code samples. Use the code
below but it does not delete all the messages.

For Each MailItem In MyFolder.Items
Debug.Print MailItem.Delete
Next MailItem

Any suggestions and/or guidance will be helpfull.

Luis
 
Do not use for each or a loop from 1 to Items.Count since deleting will
change the count.
Use "for i=Items.Count to 1 step -1" instead

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
You need to use a countdown loop instead:

c = MyFolder.Items.Count
For i = c to 1 Step -1
Set MailItem = MyFolder.Items(i)
MailItem.Delete
Next
 
Back
Top