coleyoder

New Member
Joined
Nov 29, 2018
Messages
9
Hello,

We created a macro to send out personalized emails to our customers with each individual email having an attachment of that certain customers invoice. When running the macro I seem to keep getting stuck where it says : MyAttachments.Add(attachment). Not sure if there is some way to fix this if anyone has any advice.

Thanks.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Sub SendEmailwithAttachment()


'This macro creates and sends an email for each recipient in a list with a personalized subject, body and attached file.
'Column 1 = first name
'Column 2 = last name
'Column 3 = company
'Column 4 = email address
'Column 5 = subject
'Column 6 = body
'Column 7 = file path
'Column 8 = file name


Dim OutlookApp As Outlook.Application
Dim OutlookMailItem As Outlook.MailItem
Dim MyAttachments As Outlook.Attachments
Dim filename As String
Dim path As String
Dim attachment As String




For i = 2 To Worksheets("Automated Email List").Cells(Rows.Count, 1).End(xlUp).Row
For x = 3 To Worksheets("Automated Email List").Cells(Rows.Count, 1).End(xlUp).Row
Set OutlookApp = New Outlook.Application
Set OutlookMailItem = OutlookApp.CreateItem(0)
Set MyAttachments = OutlookMailItem.Attachments
path = Worksheets("Automated Email List").Cells(x, 7).Value


filename = Worksheets("Automated Email List").Cells(i, 8)
attachment = path + filename


OutlookMailItem.To = Cells(i, 4).Value
OutlookMailItem.Subject = Cells(i, 5).Value
OutlookMailItem.Body = Cells(i, 6).Value
MyAttachments.Add (attachment)
OutlookMailItem.Display
OutlookMailItem.Send


Set OutlookMailItem = Nothing
Set OutlookApp = Nothing
Exit For
Exit For
Next x
Next i




End Sub
 
Upvote 0
Code:
Sub SendEmailwithAttachment()


'This macro creates and sends an email for each recipient in a list with a personalized subject, body and attached file.
'Column 1 = first name
'Column 2 = last name
'Column 3 = company
'Column 4 = email address
'Column 5 = subject
'Column 6 = body
'Column 7 = file path
'Column 8 = file name


Dim OutlookApp As Outlook.Application
Dim OutlookMailItem As Outlook.MailItem
Dim MyAttachments As Outlook.Attachments
Dim filename As String
Dim path As String
Dim attachment As String
'On Error Resume Next
Set OutlookApp = New Outlook.Application
    For i = 2 To Worksheets("Automated Email List").Cells(Rows.Count, 1).End(xlUp).Row
       
        Set OutlookMailItem = OutlookApp.CreateItem(0)
        Set MyAttachments = OutlookMailItem.Attachments
        path = Worksheets("Automated Email List").Cells(i, 7).Value
        
        filename = Worksheets("Automated Email List").Cells(i, 8)
        attachment = path & filename
        
        OutlookMailItem.To = Cells(i, 4).Value
        OutlookMailItem.Subject = Cells(i, 5).Value
        OutlookMailItem.Body = Cells(i, 6).Value
        MyAttachments.Add (attachment)
        OutlookMailItem.Display
        'OutlookMailItem.Send
            
    Next i
End Sub
 
Upvote 0
Used that Macro but for some reason it is still stopping at the "MyAttachments.Add (attachment)"

Not sure why I've checked the files and they seemed to be named right on excel
 
Upvote 0
It works with mine.

In my path (G) cells I used a path ending with \

Code:
[TABLE="width: 189"]
<tbody>[TR]
[TD="width: 189"]C:\Users\MyName\Documents\[/TD]
[/TR]
</tbody>[/TABLE]
And in my file () I put the name with extension
Code:
Script2.pdf

 
Last edited:
Upvote 0
If you take out the ' before
Code:
[COLOR=#333333]'On Error Resume Next[/COLOR]

the mail will be displayed without attacment if it can't...so you could maybe investigate the ones that have and the ones that have not
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

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