Excel/Outlook vba - macro to attach multiple files to an Outlook email...

EverClear

New Member
Joined
Oct 23, 2012
Messages
32
Hello!!!</SPAN>

(Using Excel 2010 & Outlook 2010)</SPAN>
I am trying to write a macro from </SPAN>Outlook</SPAN> to send an email with multiple Excel files attached to a recipient. What the macro will do is read in a separate Excel file containing a list of the filenames in Column A, the email addresses of the recipients in Column B, and the file pathway in Column C.

The macro sort of works. That is to say, it doesn't crash. The problem is that if there are *multiple* files to attach in the email, the macro will create multiple emails. So if I have 9 files to attach, the macro will create 9 emails to the same recipient.

How can I adjust my code to attach multiple files to *single* email? Here's what I have so far:
Rich (BB code):
Option Explicit
 
Sub ReadExcel()
    Dim ExcelObject             As Object
    Dim OutlookApp              As Application
    Dim NewMessage              As MailItem
    Dim NS                      As NameSpace
    Dim fName                   As String
    Dim fLoc                    As String
    Dim eAddress                As String
    Dim fNameAddress            As String
    Dim fLocAddress             As String
    Dim eAddressAddress         As String
    Dim myAttachments           As Attachments
    Dim oWB                     As Object
    Dim oWS                     As Object
    Dim bExcelCreated           As Boolean
    Dim bBookOpened             As Boolean
    Dim CellRow                 As Long
    Dim iLastRow                As Long
    Dim iLoop                   As Long
    Dim iStep                   As Long
    Dim aAttach()               As String
    Const sWBName               As String = "mailfile.xlsm"
    Const sWBPath               As String = "C: \PathGoesHere"
    Const sWSName               As String = "Sheet1"
    Const sDelim                As String = ";"
    
' Set up the spreadsheet you want to read
    On Error Resume Next
    Set ExcelObject = GetObject(, "Excel.Application")
    bExcelCreated = False
    If ExcelObject Is Nothing Then
        Set ExcelObject = CreateObject("Excel.Application")
        bExcelCreated = True
    End If
    
     </SPAN>'/// Set workbook/worksheet here</SPAN>
    If WORKBOOKISOPEN(sWBName, ExcelObject) = True Then
        Set oWB = ExcelObject.Workbooks(sWBName)
        bBookOpened = False
    Else
        Set oWB = ExcelObject.Workbooks.Open(sWBPath & sWBName)
        bBookOpened = True
    End If
    If oWB Is Nothing Then
         </SPAN>'/// Variables set wrong or file name/path have changed</SPAN>
        MsgBox "There was an error opening the file '" & sWBName & "'."
        GoTo ExitEarly
    End If
    Set oWS = oWB.Worksheets(sWSName)
    If oWS Is Nothing Then
        MsgBox "There was an error getting the sheet name in file '" & sWBName & "'."
        GoTo ExitEarly
    End If
    On Error GoTo 0
    
    </SPAN> '/// Speed up Excel app here</SPAN>
    ExcelObject.DisplayAlerts = True
    ExcelObject.EnableEvents = True
    ExcelObject.ScreenUpdating = True
    
     ' </SPAN>Read in the data and create a new message with attachment for each Excel entry</SPAN>
    CellRow = 1
    iLastRow = oWS.Cells(oWS.Rows.Count, 1).End(-4162).Row
    
    Set OutlookApp = Application
    
    For iLoop = CellRow To iLastRow
        
        aAttach() = Split(oWS.Range("A" & iLoop).Value, sDelim)
        Set NewMessage = OutlookApp.CreateItem(olMailItem)
        NewMessage.Recipients.Add oWS.Range("B" & iLoop).Value & ";"
        For iStep = LBound(aAttach) To UBound(aAttach)
            If Dir(oWS.Range("C" & iLoop).Value & "\" & Trim(aAttach(iStep)), vbNormal) <> "" Then
                NewMessage.Attachments.Add oWS.Range("C" & iLoop).Value & "\" & Trim(aAttach(iStep))
            End If
        Next iStep
        NewMessage.Subject = ""
        NewMessage.Body = ""
        NewMessage.Display
        
    Next iLoop
    
ExitEarly:
    
    </SPAN> '/// Close Excel if we created it, otherwise restore settings</SPAN>
    If bBookOpened = True Then
        oWB.Close False
    End If
    If bExcelCreated = True Then
        ExcelObject.Quit
    Else
        ExcelObject.DisplayAlerts = True
        ExcelObject.EnableEvents = True
        ExcelObject.ScreenUpdating = True
    End If
    
End Sub
 
Function WORKBOOKISOPEN(wkbName As String, oApp As Object) As Boolean
    On Error Resume Next
    WORKBOOKISOPEN = CBool(oApp.Workbooks(wkbName).Name <> "")
    On Error GoTo 0
End Function
 
Last edited by a moderator:
My fault - I read the code, not the question!

If the cell in column A has a list of attachments separated by semicolons, so there's only one line per recipient, it looks like it should do what is needed.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Glad to help.

Welcome to the forum, too. :)
 
Upvote 0
Hi Rory,

Having just tried to actually send the emails, it would appear that although the To.. box is filled with the correct email address it is not recognised by Outlook as an email address? Do you have any idea why this might be? Should the formatting be a particular way for the email addresses in column A? I have tried putting a semi colon after the email address but no luck.
 
Upvote 0
What do you actually have in column B - recipient names as you would see in an Exchange organisation, or smtp email addresses? If it's the latter, try changing this line:
Code:
NewMessage.Recipients.Add oWS.Range("B" & iLoop).Value & ";"
to:
Code:
NewMessage.To = oWS.Range("B" & iLoop).Value
 
Upvote 0
Hello!!!

(Using Excel 2010 & Outlook 2010)
I am trying to write a macro from Outlook to send an email with multiple Excel files attached to a recipient. What the macro will do is read in a separate Excel file containing a list of the filenames in Column A, the email addresses of the recipients in Column B, and the file pathway in Column C.

The macro sort of works. That is to say, it doesn't crash. The problem is that if there are *multiple* files to attach in the email, the macro will create multiple emails. So if I have 9 files to attach, the macro will create 9 emails to the same recipient.

How can I adjust my code to attach multiple files to *single* email? Here's what I have so far:
Rich (BB code):
Option Explicit
 
Sub ReadExcel()
    Dim ExcelObject             As Object
    Dim OutlookApp              As Application
    Dim NewMessage              As MailItem
    Dim NS                      As NameSpace
    Dim fName                   As String
    Dim fLoc                    As String
    Dim eAddress                As String
    Dim fNameAddress            As String
    Dim fLocAddress             As String
    Dim eAddressAddress         As String
    Dim myAttachments           As Attachments
    Dim oWB                     As Object
    Dim oWS                     As Object
    Dim bExcelCreated           As Boolean
    Dim bBookOpened             As Boolean
    Dim CellRow                 As Long
    Dim iLastRow                As Long
    Dim iLoop                   As Long
    Dim iStep                   As Long
    Dim aAttach()               As String
    Const sWBName               As String = "mailfile.xlsm"
    Const sWBPath               As String = "C: \PathGoesHere"
    Const sWSName               As String = "Sheet1"
    Const sDelim                As String = ";"
    
' Set up the spreadsheet you want to read
    On Error Resume Next
    Set ExcelObject = GetObject(, "Excel.Application")
    bExcelCreated = False
    If ExcelObject Is Nothing Then
        Set ExcelObject = CreateObject("Excel.Application")
        bExcelCreated = True
    End If
    
  '/// Set workbook/worksheet here
    If WORKBOOKISOPEN(sWBName, ExcelObject) = True Then
        Set oWB = ExcelObject.Workbooks(sWBName)
        bBookOpened = False
    Else
        Set oWB = ExcelObject.Workbooks.Open(sWBPath & sWBName)
        bBookOpened = True
    End If
    If oWB Is Nothing Then
 '/// Variables set wrong or file name/path have changed
        MsgBox "There was an error opening the file '" & sWBName & "'."
        GoTo ExitEarly
    End If
    Set oWS = oWB.Worksheets(sWSName)
    If oWS Is Nothing Then
        MsgBox "There was an error getting the sheet name in file '" & sWBName & "'."
        GoTo ExitEarly
    End If
    On Error GoTo 0
    
   '/// Speed up Excel app here
    ExcelObject.DisplayAlerts = True
    ExcelObject.EnableEvents = True
    ExcelObject.ScreenUpdating = True
    
     ' Read in the data and create a new message with attachment for each Excel entry
    CellRow = 1
    iLastRow = oWS.Cells(oWS.Rows.Count, 1).End(-4162).Row
    
    Set OutlookApp = Application
    
    For iLoop = CellRow To iLastRow
        
        aAttach() = Split(oWS.Range("A" & iLoop).Value, sDelim)
        Set NewMessage = OutlookApp.CreateItem(olMailItem)
        NewMessage.Recipients.Add oWS.Range("B" & iLoop).Value & ";"
        For iStep = LBound(aAttach) To UBound(aAttach)
            If Dir(oWS.Range("C" & iLoop).Value & "\" & Trim(aAttach(iStep)), vbNormal) <> "" Then
                NewMessage.Attachments.Add oWS.Range("C" & iLoop).Value & "\" & Trim(aAttach(iStep))
            End If
        Next iStep
        NewMessage.Subject = ""
        NewMessage.Body = ""
        NewMessage.Display
        
    Next iLoop
    
ExitEarly:
    
     '/// Close Excel if we created it, otherwise restore settings
    If bBookOpened = True Then
        oWB.Close False
    End If
    If bExcelCreated = True Then
        ExcelObject.Quit
    Else
        ExcelObject.DisplayAlerts = True
        ExcelObject.EnableEvents = True
        ExcelObject.ScreenUpdating = True
    End If
    
End Sub
 
Function WORKBOOKISOPEN(wkbName As String, oApp As Object) As Boolean
    On Error Resume Next
    WORKBOOKISOPEN = CBool(oApp.Workbooks(wkbName).Name <> "")
    On Error GoTo 0
End Function

hi Friends i have tried the above mentioned code to create a micro. but its giving me the error. i am not able to run the same could you please help me to create/or you can create a mircro for me. that will help me to send the PDF files to separate recipients " i have data in Recipient Name in Coloum A, Recipient Mail ID in Coloum B, & File path in Coloum C. tell me if i have to make changes in Data file or code..

Thanks in advance for your help.

Regards
Rakesh
 
Upvote 0
hi Andrew, when I tries the micro with outlook its giving me the issue related to below lines, there is an error " there was an error opening the file "mailfile.xlsm"


Const sWBName As String = "mailfile.xlsm"
Const sWBPath As String = "C: \PathGoesHere"
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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