Skip to content

Attachments


Overview

The Attachments property of a mail item provides access to the collection of file attachments associated with the email. It allows users to add, remove, and manage attachments for an email message.

Syntax

vba
mailItem.Attachments

Return Value

An Attachments collection object containing the attachments for the MailItem.

Example

This example demonstrates how to create a mail item and work with attachments:

vba
Sub AttachmentsExample()
    Dim app As New nlsMailer.Application
    Dim mailItem As Object
    Dim attachments As Object

    ' Create a new mail item using the Application object
    Set mailItem = app.CreateItem(olMailItem)

    ' Access the attachments collection
    Set attachments = mailItem.Attachments

    ' Add an attachment
    Dim newAttachment As Object
    Set newAttachment = attachments.Add("C:\Documents\Report.pdf")

    ' Check the number of attachments
    Debug.Print "Total Attachments: " & attachments.Count

    ' Iterate through attachments
    Dim i As Long
    Dim attachment As Object
    For i = 1 To attachments.Count
        Set attachment = attachments.Item(i)

        ' Display attachment details
        Debug.Print "Attachment " & i & ": " & attachment.DisplayName
        Debug.Print "File Name: " & attachment.fileName
        Debug.Print "Size: " & attachment.Size & " bytes"
    Next i

    ' Remove an attachment if needed
    If attachments.Count > 0 Then
        attachments.Item(1).Delete
    End If
End Sub