?
Solved

Remove existing attachments from excel mail envelope with VBA

Posted on 2010-11-29
4
Medium Priority
?
4,022 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

718 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