Solved

Remove existing attachments from excel mail envelope with VBA

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA routine modification/tweak needed 10 54
78 files, need to delete row 2 in every file 3 33
move line without macro or copy/paste 6 45
copy down array 24 35
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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