I'm trying to delete a mailitem using the outlook API. Like the following,
Dim objMail
For each objMail in objFolder.Items
objMail.Delete
Next
Obviously, deleting an item straight away is to simple. Outlook just move it into the "Deleted Items" folder instead of deleting it. I tried to get the "Deleted Items" folder using
OutlookNameSpace.GetDefaultFolder(olDeletedItems)
and delete the mail again, but the PST the code is working on is not the default mailbox and the folder returned is the wrong deleted items folder. How can I permanently delete this mailitem?
I tried to loop through all folders in the current store, but there's no way of telling which folder is the deleted items folder except by comparing names, I can't do that since the programs will be used in multiple languages and the name is different for each version.
PS: I cannot use a third party dll :(
Help!
First problem of your code is not appropriate loop you use. If you want to delete (almost anything in VBA) you need to loop your collection from the last element to first. If not, you change the order of the collection- after you delete 1st element >> 2nd one is moved to 1st position and will not be deleted.
Therefore this code should delete all items from your
DeltetedItems folder
:Obviously, you could prepare similar code for deleting from any other folder. You will run both deletion loops to remove items for sure.
Some additional remarks for
MailItem.Delete Method
from MSDN:Edit due to some comments from OP.
Even if you need to delete some items (not all) remember to use the loop type I presented above. If you need to refer to any other
DeletedItems folder
in other stores you can find this folder in these ways:I don't know if this works with all Outlook versions but it's working with Outlook 2010.