Multiple Attachments on Excel Mail Loop. Only Want One Attachment. Help!

Chrissyb173

New Member
Joined
Jul 21, 2015
Messages
3
Hello there!

I have created a macro which will create a draft email and save it, with an attachment, in a specified folder. This has been looped in order to recreate this process a few hundered times if necessary.

The macro works well with the first email however when it goes to attach the second attachment to the second email, it also attaches the first attachment. The same happens with the third line of data where the macro attaches the first and second attachment as well as the third.

Does anyone know how to get the macro to just attach the latest attachment?

Here's my macro....


Code:
Sub Email_Save_In_Folder()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim x As Integer
    x = 1
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
Do While Cells(x, 1).Value > 1
With OutMail
 .to = Cells(x, 2).Value
 .CC = ""
 .BCC = ""
 .Subject = "Payroll Reports - " & Cells(x, 1).Value
 .HTMLBody = "<BODY style=font-size:12pt;font-family:Arial>Hello,<p>Please see the attached Payroll Reports for this month.<p>Many thanks,<p>Schools Service Centre</BODY>"
 .Attachments.Add ("[URL="file://\\chesfs05\CSPayroll\Support\Academy"]\\chesfs05\CSPayroll\Support\Academy[/URL] Downloads\16-17TEST\" & Format(Date, "MM.YYYY - MMMM") & "\LBA\" & Cells(x, 1).Value & "\" & Cells(x, 1).Value & ".zip")
 .SaveAs ("[URL="file://\\chesfs05\CSPayroll\Support\Academy"]\\chesfs05\CSPayroll\Support\Academy[/URL] Downloads\16-17TEST\" & Format(Date, "MM.YYYY - MMMM") & "\LBA\" & Cells(x, 1).Value & "\" & Cells(x, 1).Value & ".msg")
End With
 
x = x + 1
Loop
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Any help would be greatly appreciated - thanks!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi and welcome to the MrExcelMessage Board.

I have to say that I can't get the code to compile with the message text as you have it.

However, ignoring that, you need to move the CreateItem inside the loop. What is happening now is that the same MailItem is being used for each iteration so the attachments are mounting up.

If you create a new MailItem each time then you will start out with an empty one each time.

Code:
Sub Email_Save_In_Folder()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim x As Integer
    x = 1
    Set OutApp = CreateObject("Outlook.Application")
    
Do While Cells(x, 1).Value > 1
Set OutMail = OutApp.CreateItem(0)
With OutMail
 .to = Cells(x, 2).Value
 .CC = ""
 .BCC = ""
 .Subject = "Payroll Reports - " & Cells(x, 1).Value
 .HTMLBody = "Hello,Please see the attached Payroll Reports for this month.
Many thanks,
Schools Service Centre"
 .Attachments.Add ("\\chesfs05\CSPayroll\Support\Academy Downloads\16-17TEST\" & Format(Date, "MM.YYYY - MMMM") & "\LBA\" & Cells(x, 1).Value & "\" & Cells(x, 1).Value & ".zip")
 .SaveAs ("\\chesfs05\CSPayroll\Support\Academy Downloads\16-17TEST\" & Format(Date, "MM.YYYY - MMMM") & "\LBA\" & Cells(x, 1).Value & "\" & Cells(x, 1).Value & ".msg")
End With
 
x = x + 1
Loop
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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