Excel

Send Excel sheet as email attachment using worksheet data.

Ease of Use

Intermediate

Version tested with

2000, 2003 

Submitted by:

Erays

Description:

This macro emails the active worksheet as an attachment naming it by the value of a named cell to addresses written into the code 

Discussion:

Where it is necessary to email a spreadsheet on a regular basis, this function allows you to code in all the recipients and also uses a named range to create a filename for the attached sheet. With slight modifications, recipients etc. can also be named in the workbook giving the function even greater flexibility. 

Code:

instructions for use

			

Option Explicit Sub EmailandSaveCellValue() 'Variable declaration Dim oApp As Object, _ oMail As Object, _ WB As Workbook, _ FileName As String, MailSub As String, MailTxt As String '************************************************* ******** 'Set email details; Comment out if not required Const MailTo = "some1@someone.com" Const MailCC = "some2@someone.com" Const MailBCC = "some3@someone.com" MailSub = "Please review " & Range("Subject") MailTxt = "I have attached " & Range("Subject") '************************************************* ******** 'Turns off screen updating Application.ScreenUpdating = False 'Makes a copy of the active sheet and save it to 'a temporary file ActiveSheet.Copy Set WB = ActiveWorkbook FileName = Range("Subject") & " Text.xls" On Error Resume Next Kill "C:\" & FileName On Error Goto 0 WB.SaveAs FileName:="C:\" & FileName 'Creates and shows the outlook mail item Set oApp = CreateObject("Outlook.Application") Set oMail = oApp.CreateItem(0) With oMail .To = MailTo .Cc = MailCC .Bcc = MailBCC .Subject = MailSub .Body = MailTxt .Attachments.Add WB.FullName .Display End With 'Deletes the temporary file WB.ChangeFileAccess Mode:=xlReadOnly Kill WB.FullName WB.Close SaveChanges:=False 'Restores screen updating and release Outlook Application.ScreenUpdating = True Set oMail = Nothing Set oApp = Nothing End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code from above in the code window that opens up.
  5. Close the VBE (Alt + Q or press the X in the top-right corner).
  6. To run go to Tools/Macros/EmailandSaveCellValue
 

Test the code:

  1. Download the zip file, open the excel attachment and Click the send page button
  2. It will open up Outlook and compose an email with the active sheet as an attachment using the sample addresses written into the code
 

Sample File:

EmailwithCellName.zip 9.93KB 

Approved by mdmackillop


This entry has been viewed 284 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express