PDA

View Full Version : Macro to send email with multiple attachment



exxelcheckkk
07-06-2012, 03:28 PM
hi every one,

I have the below codes which works very well, however, i was just wondering if it is possible to add multiple attachments instead of just one attachment per email. every thing else in the code is fine, just to add additional code to add multiple attachments. for example, in the below macro, outlook picks up information from cell a, cell b and cell c in an excel file containing file name, email address of recipient and file path. Now is it possible that multiple file names can be put in cell A to attach to a single email? or any other way to do this. Because I have list of clients to whom i send multiple files to each of them. The below macro only allows me to send one file in one email and i have to send each client multiple emails for each attachment.

please help me in this as this will really solve my problem if the below code can be modified to include multiple attachments.

thanks and best regards,
CJ


Macro:

Sub ReadExcel()
Dim ExcelObject As Object
Dim OutlookApp As Outlook.Application
Dim NewMessage As Outlook.MailItem
Dim OutlookNamespace As Outlook.NameSpace
Dim fName, fLoc, eAddress As String
Dim fNameAddress, fLocAddress, eAddressAddress As String

' Set up the spreadsheet you want to read
On Error Resume Next
Set ExcelObject = GetObject(, "Excel.Application")
If Not Err.Number = 0 Then
MsgBox "You need to have Excel running with the appropriate spreadsheet open first", vbCritical, "Excel Not Running"
End
End If

' Read in the data and create a new message with attachment for each Excel entry
CellRow = 1
Set OutlookApp = Outlook.Application
Do Until ExcelObject.Range(fNameAddress) = ""
fNameAddress = "A" & CellRow
eAddressAddress = "B" & CellRow
fLocAddress = "C" & CellRow
fName = ExcelObject.Range(fNameAddress)
fLoc = ExcelObject.Range(fLocAddress)
eAddress = ExcelObject.Range(eAddressAddress)
fName = fLoc & "\" & fName
Set OutlookApp = Outlook.Application
Set NewMessage = OutlookApp.CreateItem(olMailItem)
Set myAttachments = NewMessage.Attachments
myAttachments.Add fName
With NewMessage
.Recipients.Add eAddress
.Attachments = fName
.Display
' .Subject = "Put your subject here"
' .Send
End With
CellRow = CellRow + 1
fNameAddress = "A" & CellRow
Loop
End Sub

CatDaddy
07-06-2012, 03:33 PM
Dim fname() As String
'...
For i = LBound(fname) to Ubound(fname)
myAttachments.Add fName(i)
Next i

exxelcheckkk
07-06-2012, 03:52 PM
Thank you so much for your quick help. however, i am having trouble incorporating the above code in the macro.

I am new to VB so can you please modify the code and put the modification at their right place?

moreover, the file from which the macro reads information is attached above. in this file, in cell A, i want to include multiple file, not only one file name. I have multiple files in temp and i want to attach all of them in this email.

thanks in advance for your time.

regards,
CJ