Solved

Remove existing attachments from excel mail envelope with VBA

Posted on 2010-11-29
4
3,700 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tags from access to excel 3 26
Match formula returns N/A 5 25
Excel for Mac - How make those Tabs larger? 2 31
Create Excel formula on dynamic data 5 30
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

930 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

12 Experts available now in Live!

Get 1:1 Help Now