Solved

Remove existing attachments from excel mail envelope with VBA

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

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

623 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