Trevor G
Well-known Member
- Joined
- Jul 17, 2008
- Messages
- 6,847
- Office Version
- 365
- Platform
- Windows
Hi I am trying to send multiple attachments form Excel VBA to Outlook, I can send single attachments but when I add (what I thought would work) the code for multiple attachments it only attaches the last item and not the first. The code I am using is as follows:
Code:
[face=Courier New][COLOR=darkblue]Sub[/COLOR] EmailListNew()
[COLOR=green]'Column G holds the email addresses[/COLOR]
[COLOR=green]'Column A holds the salutation[/COLOR]
[COLOR=green]'Column K holds Yes or No to send[/COLOR]
[COLOR=green]'Adjusted code by Trevor G October 2012[/COLOR]
[COLOR=darkblue]Dim[/COLOR] OutApp [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
[COLOR=darkblue]Dim[/COLOR] OutMail [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
[COLOR=darkblue]Dim[/COLOR] cell [COLOR=darkblue]As[/COLOR] Range
[COLOR=darkblue]Dim[/COLOR] myFileList [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]Set[/COLOR] OutApp = CreateObject("Outlook.Application")
myFileList(0) = "C:\Users\Trevor G\ Article 2012.doc"
myFileList(1) = "C:\Users\Trevor G\broucher2012.pdf"
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] cleanup
Sheets("Availability").Activate
[COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] cell [COLOR=darkblue]In[/COLOR] Columns("G").Cells.SpecialCells(xlCellTypeConstants)
[COLOR=darkblue]If[/COLOR] cell.Value [COLOR=darkblue]Like[/COLOR] "?*@?*.?*" And _
LCase(Cells(cell.Row, "K").Value) = "yes" [COLOR=darkblue]Then[/COLOR]
[COLOR=darkblue]Set[/COLOR] OutMail = OutApp.CreateItem(0)
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
[COLOR=darkblue]With[/COLOR] OutMail
.To = cell.Value
.Subject = Cells(cell.Row, "H").Value
.Body = "Dear " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
Cells(cell.Row, "I").Value _
& vbNewLine & vbNewLine & _
Cells(cell.Row, "J").Value
[COLOR=green]'This is the code which is to send multiple attachments and fails[/COLOR]
[COLOR=green]'It is only sending the last Item in the list[/COLOR]
[COLOR=darkblue]For[/COLOR] i = [COLOR=darkblue]LBound[/COLOR](myFileList) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](myFileList)
.Attachments.Add myFileList(i)
[COLOR=darkblue]Next[/COLOR] i
[COLOR=green]'.Send 'Or use .Display[/COLOR]
.Display
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
[COLOR=darkblue]Set[/COLOR] OutMail = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] cell
cleanup:
[COLOR=darkblue]Set[/COLOR] OutApp = [COLOR=darkblue]Nothing[/COLOR]
Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/face]