PC Review


Reply
Thread Tools Rate Thread

Attach file to e-mail using excel macro

 
 
JT1977
Guest
Posts: n/a
 
      13th Aug 2009
I'm writing a macro in Excel to connect to a button on an MS Excel 2007
spreadsheet. When clicked the button copies some data from the file and uses
the data to create a new .txt file. Since the macro is creating the file I
can control the file name and storage location. Once created I want to
e-mail the text file to a standard e-mail address. I'm using the code below
to open an e-mail editor and compose the message. I'm using some variables
to to dictate the list of receipients, subject line, etc. based on some
criteria in the file itself.

ActiveWorkbook.EnvelopeVisible = True


With ActiveSheet.MailEnvelope
.Introduction = Range("T22")
.Item.To = Range("T15")
.Item.CC = Range("T16")
.Item.Subject = "CST CARRIERS"
.Item.Send
End With

What I would like to do is be able to attach the .txt file to the e-mail
through the code of the macro. I was hoping there was some simple code like
..Item.Attachment = "blah" that would attach the file. I can't find the code
to do this. Can you tell me what the code is to do this?

I know the e-mail generated by the above code is capable of including
attachments because when I put a "stop" line in the code and run it, the
e-mail editor that opens has a button to attach files. If I end the debugger
at this point I can manually seacrh for and attach the file to the note using
this button.

 
Reply With Quote
 
 
 
 
arjen van...
Guest
Posts: n/a
 
      13th Aug 2009
Try something like this:

Sub NewMail()

ActiveWorkbook.EnvelopeVisible = True

Dim strAttachment As String
strAttachment = "C:\customers.txt"

With ActiveSheet.MailEnvelope
.Introduction = Range("T22")
.Item.To = Range("T15")
.Item.CC = Range("T16")
.Item.Subject = "CST CARRIERS"
.Item.Send
.Item.Attachments.Add strAttachment
End With

End Sub

Though I think creating the string variable isn't essential, you could just
put the path directly after .Add
 
Reply With Quote
 
JT1977
Guest
Posts: n/a
 
      24th Aug 2009
Thanks for the help. This was what I was looking for. However if I execute
this code multiple times it is almost as if the file to be attached is saved
to a clipboard. Each subsequent time I execute the code the previously
attached file is also added. i.e. If I run the code a second time it not
only attaches the file selected by the code but also the file selected by the
code when run previously. Is there a way to "clear" the clipboard so only
the one file is attached?

"arjen van..." wrote:

> Try something like this:
>
> Sub NewMail()
>
> ActiveWorkbook.EnvelopeVisible = True
>
> Dim strAttachment As String
> strAttachment = "C:\customers.txt"
>
> With ActiveSheet.MailEnvelope
> .Introduction = Range("T22")
> .Item.To = Range("T15")
> .Item.CC = Range("T16")
> .Item.Subject = "CST CARRIERS"
> .Item.Send
> .Item.Attachments.Add strAttachment
> End With
>
> End Sub
>
> Though I think creating the string variable isn't essential, you could just
> put the path directly after .Add

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
!!Macro to attach pdf and reference excel!! Marlene.Sawhney@gmail.com Microsoft Excel Programming 1 26th Jan 2007 06:41 PM
E-mail macro - how do I attach multiple attachments? =?Utf-8?B?RyBhbmQgKGFqayk=?= Microsoft Excel Misc 1 9th Oct 2006 01:22 PM
How to attach an excel file to an Access Macro =?Utf-8?B?U3lyc2thbg==?= Microsoft Access Macros 0 18th Jul 2005 07:28 PM
How do you attach a macro in excel 2003? =?Utf-8?B?SmFtZXM=?= Microsoft Excel Misc 1 21st Apr 2005 04:51 PM
Attach File Macro Benji Microsoft Outlook VBA Programming 1 15th Feb 2005 07:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:47 PM.