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!
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

RickXL

MrExcel MVP
Joined
Sep 9, 2013
Messages
4,314
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
 

Watch MrExcel Video

Forum statistics

Threads
1,101,800
Messages
5,482,964
Members
407,371
Latest member
gdjenkins80

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top