How to send a range of cells in an e-mail message by using Visual Basic for Applications in Excel 2002 or Excel 2003 (816644)



The information in this article applies to:

  • Microsoft Office Excel 2003
  • Microsoft Excel 2002
  • Microsoft Outlook 2002

SUMMARY

In Microsoft Excel, you can send a section of a workbook as an e-mail message. This article contains sample Microsoft Visual Basic for Applications (VBA) code that demonstrates how to automate sending a range of cells in an e-mail message.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, see the following Microsoft Web site: For additional information about the support options available from Microsoft, visit the following Microsoft Web site: The following VBA code sample sends a range of ten cells, A1 through B5, from the currently active workbook to the e-mail address specified in the code:
Sub Send_Range()
   
   ' Select the range of cells on the active worksheet.
   ActiveSheet.Range("A1:B5").Select
   
   ' 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
      .Introduction = "This is a sample worksheet."
      .Item.To = "E-Mail_Address_Here"
      .Item.Subject = "My subject"
      .Item.Send
   End With
End Sub
Note This code only works with Microsoft Outlook. It does not work with any version of Microsoft Outlook Express.
Replace E-Mail_Address_Here with the e-mail address that you want to send the range to.

Note Item.Send in the code triggers an Outlook security warning to display the following message: A program is trying to automatically send e-mail on your behalf. Do you want to allow this? If this is unexpected, it may be a virus and you should choose "No". Click Yes to allow the mail to be sent.

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

290500 Description of the developer-related e-mail security features in Outlook 2002


Modification Type:MinorLast Reviewed:3/23/2006
Keywords:kbcode KbVBA kbemail kbhowto KB816644