Solved

Remove existing attachments from excel mail envelope with VBA

Posted on 2010-11-29
4
3,645 Views
Last Modified: 2012-05-10
I have the code below that sends out an email from an excel sheet and adds some attachments. However, as I run it for different attachments and recipients, the attachments sent remain in the mail envelope. So if I run the code a second time with different recipients and different attachments, the two attachments of the first execute of the code remain attached to the mail message, the two of the new code are added, so the second execution of the code sends out 4 attachments (if different from the initial 2) and so on...

In order to have the code work properly, I need to exit excel every time after running it and start it up again / open the file.

So it seems when I execute the code multiple times it is almost as if the file(s) to be attached are saved to a clipboard. Each subsequent time I execute the code the previously attached file is also added. Is there a way to "clear" the clipboard so only
the one file is attached?

Found this here but does not work properly yet with my code:

neither this:

cp1 = .Item.Attachments.Count
If cp1 <> 0 Then
For cp = 1 To .Item.Attachments.Count
.Item.Attachments(1).Delete
Next
End If

nor this:

For cp = 1 To .Item.Attachments.Count
.Item.Attachments(1).Delete
Next

seems to work with my code... any suggestion/help appreciated.




Sub ReportsSend()

   

   'Application.ScreenUpdating = False

   'Application.DisplayAlerts = False

   

   Sheets("mailer").Select

   Range("ReportBody").Select

   

   Dim MailToAddress As String

    MailToAddress = Range("ReportTo").Value

   Dim MailCcAddress As String

    MailCcAddress = Range("ReportCc").Value

   Dim MailSubject As String

    MailSubject = Range("ReportSubject").Value

   Dim ReportAttachment1 As String

   Dim ReportAttachment2 As String

   

   ReportAttachment1 = Range("Report1").Value

   ReportAttachment2 = Range("Report2").Value

      

   ' Show the envelope on the ActiveWorkbook.

   ActiveWorkbook.EnvelopeVisible = True

      

   ' Set the optional introduction field thats adds

   ' some header text to the email body. It also sets

   ' the To and Subject lines. Finally the message

   ' is sent.

   With ActiveSheet.MailEnvelope

      .Item.To = MailToAddress

      .Item.CC = MailCcAddress

      .Item.Subject = MailSubject

      .Item.Attachments.Add ReportAttachment1

      .Item.Attachments.Add ReportAttachment2

      .Item.Send

   End With



End Sub

Open in new window

0
Comment
Question by:stmoritz
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 250 total points
ID: 34232010
You should always loop backwards if you are deleting items:

For cp = .Item.Attachments.Count to 1 step -1
.Item.Attachments(cp).Delete
Next cp

Open in new window

0
 

Author Comment

by:stmoritz
ID: 34232093
Thanks Rorya. I just realised that I did not mention that I get an error when running the approaches to remove the attachments above and the same is true with your version:

"invalid or unqualified reference" is displayed as error and it first highlights ".Item" in the first line of "For cp = .Item.Attachments.Count To 1 Step -1" and then the cursor goes to the name of the sub and marks it yellow...

0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 250 total points
ID: 34232159
Where did you put it - it needs to go between lines 28 and 35 in your posted code.
0
 

Author Comment

by:stmoritz
ID: 34232371
I knew that I am weak... of course!!! Thanks a lot rorya, works perfect!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Copying and pasting pictures from Excel 2 22
Excel 3 24
Moving Range Names/Formulas over to new WB 21 16
Macro to delete column 3 11
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now