Adding Attachments with VBA

patrickliuhhs

New Member
Joined
Jul 28, 2014
Messages
1
Hi All,

I'm somewhat new to VBA so bear with me. I'm trying to use VBA to automate emails with personalized attachments for each person. To do this, I created an excel spreadsheet with the person's name in the first column, email address in the second, and path to the attachment in the third.

I haven't been able to get the code to work but I feel like I'm close.

Code:
Sub AttachSend()

Dim objMail As Outlook.MailItem
Dim intX As Integer
Dim FileCount As Integer
Dim MailAttachment As String
Dim MailAddress As String


FileCount = Application.WorksheetFunction.CountA(Range("C2:C200"))
Set objMail = Outlook.Application.CreateItem(olMailItem)


For intX = 2 To FileCount
    With Application.ActiveSheet
        Set MailAttachment = Cells(intX, 3).Value
        Set MailAddress = Cells(intX, 2).Value
    
    With objMail
        .Subject = MailAddress.Value
        .Body = "Message Body"
        .Recipients.Add MailAdress.Value
        .Attachments.Add MailAttachment.Value
        .Display
    End With
    Set objMail = Nothing


Next


End Sub

When I run this code, I receive an "Object Required" error with the line "Set MailAttachment =" highlighted. I unfortunately haven't been able to figure out what the issue is so you guys are like my last hope.

Thanks in advance!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Set is only used with objects. Try:
Code:
With Application.ActiveSheet 
   MailAttachment = .Cells(intX, 3).Value         
   MailAddress = .Cells(intX, 2).Value 
End With
and then:
Code:
.Recipients.Add MailAddress
.Attachments.Add MailAttachment
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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