Class Attachments (Outlook VBA)

Contains a set of Attachment objects that represent the attachments in an Outlook item. To use a Attachments class variable it first needs to be instantiated, for example


Dim atts as Attachments
Set atts = Session.CreateSharingItem.Move.GetStorage.Attachments

For Each

Here is an example of processing the Attachments items in a collection.


Dim att As Attachment
For Each att In Session.CreateSharingItem.Move.GetStorage.Attachments
	
Next att

Add

Creates a new attachment in the Attachments collection.

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.

Add (Source, Type, Position, DisplayName)


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

Arguments

The following argument is required

Source (Object) - 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.

Optional arguments

The following arguments are optional

Type (Long) - The type of the attachment. Can be one of the OlAttachmentType constants.

Position (Long) - This parameter applies only to email 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 email item specifies that the attachment should be placed at the end. A value of 0 makes the attachment hidden.

DisplayName (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.

Class

Returns an OlObjectClass constant indicating the object's class. Here you can find possible values for OlObjectClass.


Dim oocsClass As OlObjectClass
oocsClass = Session.CreateSharingItem.Move.GetStorage.Attachments.Class

Count

Returns a Long indicating the count of objects in the specified collection.


Dim lngCount As Long
lngCount = Session.CreateSharingItem.Move.GetStorage.Attachments.Count

Item

Returns an Attachment object from the collection.

Item (Index)

Index: Either the index number of the object, or a value used to match the default property of an object in the collection.


Dim att As Attachment
Set att = Session.CreateSharingItem.Move.GetStorage.Attachments(Index:=1)

Remove

Removes an object from the collection.

Remove (Index)

Index: The 1-based index value of the object within the collection.


Sub RemoveAttachmentBeforeForwarding() 
 
 Dim myinspector As Outlook.Inspector 
 
 Dim myItem As Outlook.MailItem 
 
 Dim myattachments As Outlook.Attachments 
 
 
 
 Set myinspector = Application.ActiveInspector 
 
 If Not TypeName(myinspector) = "Nothing" Then 
 
 Set myItem = myinspector.CurrentItem.Forward 
 
 Set myattachments = myItem.Attachments 
 
 While myattachments.Count > 0 
 
 myattachments.Remove 1 
 
 Wend 
 
 myItem.Display 
 
 myItem.Recipients.Add "Dan Wilson" 
 
 myItem.Send 
 
 Else 
 
 MsgBox "There is no active inspector." 
 
 End If 
 
End Sub

Session

Returns the NameSpace object for the current session.

The Session property and the GetNamespace method can be used interchangeably to obtain the NameSpace object for the current session. Both members serve the same purpose. For example, the following statements do the same function: