+ Reply to Thread
Results 1 to 2 of 2

Attach multiple files to an email

Hybrid View

  1. #1
    Registered User
    Join Date
    11-29-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2010
    Posts
    28

    Attach multiple files to an email

    Hi guys i have this code
    Const msoFileDialogOpen = 1
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objWord = CreateObject("Word.Application")
    Set WshShell = CreateObject("WScript.Shell")
    strInitialPath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\"
    objWord.ChangeFileOpenDirectory (strInitialPath)
    With objWord.FileDialog(msoFileDialogOpen)
    .Title = "Select the file to process"
    .AllowMultiSelect = True
    .Filters.Clear
    .Filters.Add "All Files", "*.*"
    .Filters.Add "Excel Files", "*.xls;*.xlsx"
    .Filters.Add "Text Files", "*.txt"
    .Filters.Add "Various Files", "*.xls;*.doc;*.vbs"
     If .Show = -1 Then
    For Each File In .SelectedItems
    Set objFile = fso.GetFile(File)
    
    filepath = objFile
    Next
    Else
    End If
    End With
    This code allow me to attach 1 file to email, however i can only attach 1 file how can i do to attach multiple files?

    Thanks,

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    re: Attach multiple files to an email

    Hi there,

    Try the following code and see if it does what you want:


    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Public Sub LaunchOutlook()
    
        Const iMAIL_ITEM    As Integer = 0
    
        Dim objMailItem     As Object
        Dim vaFileNames     As Variant
        Dim objOutlook      As Object
        Dim iFileNo         As Integer
    
        On Error Resume Next
            Set objOutlook = GetObject(Class:="Outlook.Application")
        On Error GoTo 0
    
        If objOutlook Is Nothing Then
            Set objOutlook = CreateObject(Class:="Outlook.Application")
        End If
    
        vaFileNames = mvaFilenames()
    
        With objOutlook
    
            Set objMailItem = .CreateItem(iMAIL_ITEM)
            With objMailItem
    
                .To = "This is the Addressee name"
                .Subject = "This is the Subject of the email"
                .Body = "This is the body of the email"
    
    '           Disable error handling for the next step - this avoids an error which
    '           occurs if the cancel button on the "Select Profile" dialog box is pressed
                On Error Resume Next
    
                    If Not IsEmpty(vaFileNames) Then
    
                        For iFileNo = 1 To UBound(vaFileNames, 1)
                            .Attachments.Add vaFileNames(iFileNo)
                        Next iFileNo
    
                    End If
    
                On Error GoTo 0
    
            .Display
    
            End With
    
        End With
    
        Set objMailItem = Nothing
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Function mvaFilenames() As Variant
    
        Const iFILE_DIALOG_OPEN As Integer = 1
    
        Dim strInitialPath      As String
        Dim vaFileNames         As Variant
        Dim vFileName           As Variant
        Dim iFileNo             As Integer
    
        Dim objShell            As Object
        Dim objWord             As Object
        Dim objFSO              As Object
    
        Set objShell = CreateObject("WScript.Shell")
        Set objWord = CreateObject("Word.Application")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        strInitialPath = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\"
    
        objWord.ChangeFileOpenDirectory (strInitialPath)
    
        With objWord.FileDialog(msoFileDialogOpen)
    
            .Title = "Select the file to process"
            .AllowMultiSelect = True
            .Filters.Clear
            .Filters.Add "All Files", "*.*"
            .Filters.Add "Excel Files", "*.xls;*.xlsx"
            .Filters.Add "Text Files", "*.txt"
            .Filters.Add "Various Files", "*.xls;*.doc;*.vbs"
    
            If .Show = -1 Then
    
                 ReDim vaFileNames(1 To .SelectedItems.Count)
                 iFileNo = 1
    
                For Each vFileName In .SelectedItems
    
                    vaFileNames(iFileNo) = objFSO.getfile(vFileName)
                    iFileNo = iFileNo + 1
    
                Next
    
            End If
    
        End With
    
        mvaFilenames = vaFileNames
    
    End Function

    I hope this helps - please let me know how you get on with it.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel Email Macro - HELP! - Need to be able to attach two different files
    By benwahchang in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2013, 08:41 AM
  2. SaveAs PDF and attach to email macro won't attach?!
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2012, 05:28 PM
  3. Application.GetOpenFilename, Attach files to Email, Move files, Delete Original.
    By D_Rennie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-08-2009, 12:11 AM
  4. Modifing code to attach files to email
    By jat82nd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-20-2007, 01:41 PM
  5. Attach all Open Workbooks to email as separate files?
    By nbaj2k in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-03-2006, 09:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1