Adding multiple attachments using VBA

Mychaltb

New Member
Joined
Nov 15, 2016
Messages
16
Hello,

I am new to VBA and have pieced together a code to send multiple emails. However, I am struggling with how I can add additional attachments (some of them are excel files and some are PDF files) to each of the emails. Any tips would be greatly appreciated. My code is below:

Sub Send_email_fromexcel()
Dim edress As String
Dim CC As String
Dim subj As String
Dim message As String
Dim filename As String
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myattachments As Object
Dim path As String
Dim Body As String
Dim lastrow As Integer
Dim attachment As String
Dim x As Integer


x = 2
Do While Sheet2.Cells(x, 1) <> ""
'For x = 2 To 4


Set outlookapp = CreateObject("outlook.application")
Set outlookmailitem = outlookapp.createitem(0)
Set myattachments = outlookmailitem.attachments
path = "C:\Users\mychal\Desktop"




edress = Sheet2.Cells(x, 1)
CC = Sheet2.Cells(x, 2)
subj = Sheet2.Cells(x, 3)
filename = Sheet2.Cells(x, 4)
Body = Sheet2.Cells(x, 5)
attachment = path + filename






outlookmailitem.To = edress
outlookmailitem.CC = CC
outlookmailitem.bcc = ""
outlookmailitem.Subject = subj
outlookmailitem.Body = Body


myattachments.Add (attachment)
outlookmailitem.display

lastrow = lastrow + 1
edress = ""
x = x + 1


Loop




Set outlookapp = Nothing
Set outlookmailitem = Nothing




End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hello, you can call MailItem.Attachments.Add as many times as you like. You've already done it once in your code; just do it again with a different path afterwards...
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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