Attaching Multiple Files to Email Document

rigate2003

New Member
Joined
Sep 9, 2009
Messages
4
Using VBA and Excel, I am trying to attach multiple documents to an email. The user will input the file search criteria, and any documents matching that file search criteria will be added as attachments. Currently, my macro perform a file search and open a new email in Outlook, but I'm not sure how to loop through and do multiple attachments. My code will need to do the following:

Loop through all files in a folder
If filename matches user inputted search then
attach file to an already created email document
end if
Check Next file

Any suggestions???
Thanks!! :p
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Loop through the search results, every time it hits the file add the attachement with :
.Attachments.Add (myFileName)
 
Upvote 0
Thanks for your response! I have the following code in place, but still get an error. Any suggestions? Do I need to declare/define "Filename" maybe? I appreciate the help!

i = 1
Do Until i = .FoundFiles.Count
OutMail.attachments.Add Filename
i = i + 1
Loop
 
Upvote 0
Try this.

Code:
'Put this towards the top, outside the email building section
    Dim sFName As String
    Dim myDir As String
    myDir = "C:\Temp\" 'Set Dir to search
    ChDir (myDir) 'Change to that dir
    sFName = Dir(myDir & "Copy*.xls") 'Set Search spec


'Then in the email building section
                Do While Len(sFName) > 0
                    .Attachments.Add (myDir & "\" & sFName)
                    sFName = Dir
                Loop
 
Upvote 0
Thanks for your response! I have the following code in place, but still get an error. Any suggestions? Do I need to declare/define "Filename" maybe? I appreciate the help!

i = 1
Do Until i = .FoundFiles.Count
OutMail.attachments.Add Filename
i = i + 1
Loop
Since you're using the FoundFiles object from Application.FileSearch, the line to attach each file should be:
OutMail.attachments.Add .FoundFiles(i)

Here's a complete working example:
Code:
Option Explicit

'Need reference to Microsoft Outlook Object Library

Sub Create_Email_With_Attachments()

    Dim olApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim fs As FileSearch
    Dim i As Integer
        
    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.CreateItem(olMailItem)
    
    With olMail
        .To = "recipient.email@address.com"  'CHANGE AS NECESSARY
        .Subject = "Test"   'CHANGE AS NECESSARY
        .Body = "Email body text"   'CHANGE AS NECESSARY
        .Display
    End With
    
    Set fs = Application.FileSearch
    With fs
        .LookIn = "F:\temp\excel\"   'CHANGE AS NECESSARY
        .Filename = "Test*"   'CHANGE AS NECESSARY
        If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
            For i = 1 To .FoundFiles.Count
                olMail.Attachments.Add .FoundFiles(i)
            Next
            'olMail.Send  'UNCOMMENT TO SEND EMAIL
        Else
            MsgBox "No matching files found"
        End If
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top