Delete Attachments w/C#

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?
 
K

Ken Slovak - [MVP - Outlook]

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.
 
G

Guest

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
 
D

Dan Cernoch

Also,
I am new to c# and the Outlook Object Model. I tried
counting loops but the index gave me out of scope errors.
 
D

Dan Cernoch

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
 
D

Dan Cernoch

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
 
K

Ken Slovak - [MVP - Outlook]

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
 

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