Search code examples
excelvbaoutlook

How to attach multiple files to a message in a loop?


I am trying to attach multiple files to a message in a loop. The code below should do the following:

  • open the file selector
  • I would pick or more files
  • the files would be sent by e-mail, to an address specified in my spreadsheet

I was able to pick one file and send the e-mail with it attached.
When I select two or more files the code stops in the following part:

For fichier1 = LBound(fichier1) To UBound(fichier1)
    MonMessage.Attachments.Add fichier1
Next fichier1

Note: I'm not using the reference of the Outlook object, because another macro stops working.

Full code

Sub Send_Email()

Dim Fichier As Variant
Dim A       As Integer
Dim MaMessagerie As Object
Dim MonMessage As Object
Set MaMessagerie = CreateObject("Outlook.application")
Set MonMessage = MaMessagerie.CreateItem(0)

fichier1 = Application.GetOpenFilename("File to send (*.XLS*), *.XLS*", _
               Title:="Pick at least One file", _
               MultiSelect:=True)

If Not IsArray(fichier1) Then
    If fichier1 = "" Or fichier1 = False Then
        MsgBox "No file selected!", vbExclamation, "Atention"
        Application.ScreenUpdating = True
        Exit Sub
    End If
End If

MonMessage.To = "[email protected]"
MonMessage.CC = ""

'loop to Attach 1 or more files
For fichier1 = LBound(fichier1) To UBound(fichier1)
    MonMessage.Attachments.Add fichier1
Next fichier1

MonMessage.Subject = "Subject"

MonMessage.Body = "test"
MonMessage.Display
'MonMessage.Send

Set MaMessagerie = Nothing

'MsgBox "Email sent"

End Sub

Solution

  • You never declare fichier1 in the code.

    In the following it is declared and a new variable has been added, idx, that can be used when looping through the array of selected files.

    Option Explicit
    
    Sub SendMultiAttachmebts()
    Dim Fichier1 As Variant
    Dim MaMessagerie As Object
    Dim MonMessage As Object
    Dim idx As Long
    
        Set MaMessagerie = CreateObject("Outlook.application")
        Set MonMessage = MaMessagerie.CreateItem(0)
    
        Fichier1 = Application.GetOpenFilename("File to send (*.XLS*), *.XLS*", _
                                               Title:="Pick at least One file", _
                                               MultiSelect:=True)
    
        If Not IsArray(Fichier1) Then
            If Fichier1 = "" Or Fichier1 = False Then
                MsgBox "No file selected!", vbExclamation, "Atention"
                Application.ScreenUpdating = True
                Exit Sub
            End If
        End If
    
        MonMessage.To = "[email protected]"
        MonMessage.CC = ""
    
        'loop to Attach 1 or more files
        For idx = LBound(Fichier1) To UBound(Fichier1)
    
            MonMessage.Attachments.Add Fichier1(idx)
    
        Next idx
    
        MonMessage.Subject = "Subject"
        MonMessage.Body = "test"
        MonMessage.Display
        'MonMessage.Send
    
        Set MaMessagerie = Nothing
    
        'MsgBox "Email sent"
    
    End Sub