Condividi tramite


Attachments.Add Method (Outlook)

Creates a new attachment in the Attachments collection.

Syntax

expression .Add(Source, Type, Position, DisplayName)

expression A variable that represents an Attachments object.

Parameters

Name

Required/Optional

Data Type

Description

Source

Required

Variant

The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

Type

Optional

Long

The type of the attachment. Can be one of the OlAttachmentType constants.

Position

Optional

Long

This parameter applies only to e-mail messages using the Rich Text format: it is the position where the attachment should be placed within the body text of the message. A value of 1 for the Position parameter specifies that the attachment should be positioned at the beginning of the message body. A value 'n' greater than the number of characters in the body of the e-mail item specifies that the attachment should be placed at the end. A value of 0 makes the attachment hidden.

DisplayName

Optional

String

This parameter applies only if the mail item is in Rich Text format and Type is set to olByValue: the name is displayed in an Inspector object for the attachment or when viewing the properties of the attachment. If the mail item is in Plain Text or HTML format, then the attachment is displayed using the file name in the Source parameter.

Return Value

An Attachment object that represents the new attachment.

Remarks

When an Attachment is added to the Attachments collection of an item, the Type property of the Attachment will always return olOLE (6) until the item is saved. To ensure consistent results, always save an item before adding or removing objects in the Attachments collection.

Example

The following Microsoft Visual Basic /Visual Basic for Applications (VBA) example creates a mail item, adds an attachment by embedding it at the beginning of the message body, and displays it. To run this example, make sure the attachment which is a file called Test.Doc exists in the C:\ folder.

Sub AddAttachment() 
 Dim myItem As Outlook.MailItem 
 Dim myAttachments As Outlook.Attachments 
 
 Set myItem = Application.CreateItem(olMailItem) 
 Set myAttachments = myItem.Attachments 
 myAttachments.Add "C:\Test.doc", _ 
 olByValue, 1, "Test" 
 myItem.Display 
End Sub

See Also

Concepts

How to: Attach a File to a Mail Item

How to: Attach an Outlook Contact Item to an Email Message

How to: Limit the Size of an Attachment to an Outlook Email Message

How to: Modify an Attachment of an Outlook Email Message

Attachments Object

Attachments Object Members