Attachment object

The Attachment object allows you to work with an attachment. The Attachments collection contains all of the attachments associated with a mail message.

Adding an attachment

Use the Add method to add an attachment to the Attachments collection. You must specify the full pathname of the attachment. You can also specify the attachment type, the position number of the attachment, or the display name of the attachment. The attachment type is specified with a constant from the olAttachmentType enumeration,

{Add a new attachment to the Attachments collection. The attachment is placed in the first position with a display name of "Houses".}
attachment = mailItem.Attachments.Add("C:\RESM\HOUSES.TXT", none, 1, "Houses");

Retrieving an existing attachment

You can retrieve an attachment from the Attachments collection with the Item method by the number associated with the attachment.

{Retrieve the third attachment from the Attachments collection.}
attachment = mailItem.Attachments.Item(3);

Retrieving an attachment’s index

Use the Index property to retrieve the index of the attachment in the Attachments collection.

{Retrieve the attachment’s index.}
itemIndex = attachment.Index;

Removing an attachment

Use the Delete method from the Attachment object to remove an attachment from a mail message.

{Remove the attachment from the mail message.}
attachment.Delete();

You can also use the Remove method from the Attachments collection to remove an attachment from a mail message. You must supply the number associated with the attachment.

{Delete the second attachment from the Attachments collection.}
mailItem.Attachments.Remove(2);

Saving an attachment as a file

Use the SaveAsFile method to save the attachment as a file. You must specify the filename of the file.

{Save the first attachment in the Attachments collection as a file.}
mailItem.Attachments.Item(1).SaveAsFile("C:\RESM\HOUSES.TXT");


Documentation Feedback