Delete Attachments w/C#

  • Thread starter Thread starter Dan Cernoch
  • Start date Start date
D

Dan Cernoch

I can list all attachments an "Inbox" and all sub-
Folders. However, I cannot delete the attachments out of
the MailItems. Some get deleted others do not. Any Ideas?
 
Is this on items where there are more than 1 attachments? Are you using a
down-counting For loop to do the deletions? An up-counting For loop will
change the loop counter in the loop as items are deleted from the
Attachments collection.
 
PsuedoCode Example

foreach(mailitem in Inbox)
{
foreach(attachment in mailitem.attachments)
{
attachment.delete();
}
}

I have 6 of 15 emails w/attachments
It will delete only the first attachment in the first mail
item
 
Also,
I am new to c# and the Outlook Object Model. I tried
counting loops but the index gave me out of scope errors.
 
PsuedoCode Example

foreach(mailitem in Inbox)
{
foreach(attachment in mailitem.attachments)
{
attachment.delete();
}
}

I have 6 of 15 emails w/attachments
It will delete only the first attachment in the first mail
item
 
Also, if it helps....
I can list all attachments to the Console and save them to
the Hard Disk using the foreach methodology shown earlier.

Thanks,
Dan
 
When you delete an item in a For...Each loop the loop counter is changed
within the loop as items are deleted. That is bad in any programming
language. I don't use .NET languages for Outlook but in VB 6 pseudocode the
loop would look like this:

Dim i As Long

For i = Item.Attachments.Count To 1 Step -1
Item.Attachments.Remove i
Next
 
Back
Top