Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2008, 06:57 PM
Registered User
 
Join Date: Oct 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default send emails with an Excel table as text format

Hello,

I need to create a macro that sends out emails through Outlook. Plese see my exmaple code below. Instead of sending an attachment, is it possible to copy the content of the excel spreadsheet and paste it directly to the email body.

many thanks in advance.

Peter

-------------------------------
Sub Email()

    Dim myOutlook As Object
    Dim myMailItem As Object
    Dim FileAttach As String

    Application.DisplayAlerts = False

    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.CreateItem(olMailItem)

    FileAttach = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name

    With olMail
        .To = "[email protected]"
        .Subject = "Test"
        .Body = "test emails"
        .Attachments.Add FileAttach
        .Send
    End With

End Sub
-------------------------
 
Old October 24th, 2008, 10:04 PM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the .sendmail method. If you use the workbook you want to send, it will go through outlook as an attachment. You can specify recipient(s) and subject. e.g.,


Excel Developer Reference
Workbook.SendMail Method

Sends the workbook by using the installed mail system.expression.SendMail(Recipients, Subject, ReturnReceipt)
expression A variable that represents a Workbook object.

Parameters

Name Required/Optional
Data Type Description
Recipients Required Variant Specifies the name of the recipient as text, or as an array of text strings if there are multiple recipients. At least one recipient must be specified, and all recipients are added as To recipients.

Subject Optional Variant Specifies the subject of the message. If this argument is omitted, the document name is used.
ReturnReceipt Optional Variant True to request a return receipt. False to not request a return receipt. The default value is False.

Example:

This example sends the active workbook to a single recipient.

Visual Basic for Applications
ActiveWorkbook.SendMail recipients:="Jean Selva"


Do you know it the sender has Outlook? If you don't know for sure, use the application.mailsystem to ensure it is xlMapi; e.g.,

Visual Basic for Applications
Select Case Application.MailSystem
    Case xlMAPI
        MsgBox "Mail system is Microsoft Mail"
    Case xlPowerTalk
        MsgBox "Mail system is PowerTalk"
    Case xlNoMailSystem
        MsgBox "No mail system installed"
End Select


 
Old October 27th, 2008, 11:34 AM
Registered User
 
Join Date: Oct 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi LBAKER,

Thanks for your reply. But it seemed to me that the .SendMail method can only apply to an object of Workbook. Therefore, the content I want to send is still sent as an attachment. I am looking for a method that can send the content of an Excel workbook directly in the body of the email in Text format.

Peter
 
Old October 29th, 2008, 11:45 AM
JP JP is offline
Authorized User
 
Join Date: Apr 2008
Posts: 57
Thanks: 1
Thanked 3 Times in 3 Posts
Default

See http://www.rondebruin.nl/mail/folder3/mail4.htm

Oh and FYI, if you are late-binding Outlook, you need to use the number constants with CreateItem.


HTH,
JP
 
Old October 29th, 2008, 04:04 PM
Registered User
 
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot, JP.

This is great!

Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel format changes from Number to Text ajindal General .NET 2 July 24th, 2007 06:56 AM
Code to send emails maitias C# 2005 2 February 1st, 2006 01:09 PM
send emails to several users femig VBScript 4 April 15th, 2005 07:42 AM
Send two emails instead of one ss2003 Beginning PHP 3 January 28th, 2005 11:54 AM
Send Emails and Attachments. mistry_bhavin General .NET 2 August 11th, 2004 10:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.