Send email message from excel
You can send an email message from Excel using VBA (Visual Basic for Applications) programming. Here's a step-by-step guide:
Prerequisites:
- You need to have Excel 2007 or later version.
- You need to have Outlook installed on your computer.
Step 1: Enable the Developer Tab
- Go to the "File" menu and click on "Options".
- In the "Excel Options" dialog box, click on the "Customize Ribbon" button.
- Check the box next to "Developer" and click "OK".
Step 2: Create a Macro
-
Go to the "Developer" tab and click on the "Visual Basic" button.
-
In the Visual Basic Editor, click on "Insert" > "Module" to insert a new module.
-
Paste the following code into the module:
Sub SendEmail() Dim olApp As Object Dim olMail As Object Set olApp = CreateObject("Outlook.Application") Set olMail = olApp.CreateItem(0) With olMail .To = "[email protected]" ' replace with recipient's email address .Subject = "Test Email from Excel" ' replace with subject line .Body = "This is a test email sent from Excel." ' replace with email body .Send End With Set olMail = Nothing Set olApp = Nothing End Sub
Step 3: Run the Macro
-
Go back to the Excel worksheet.
-
Press "Alt + F8" to open the Macro dialog box.
-
Select the "SendEmail" macro and click "Run".
Tips and Variations:
- You can modify the code to send an email with attachments by adding the following lines:
Dim olAttach As Object Set olAttach = olMail.Attachments.Add("C:\Path\To\Attachment.xlsx")
- You can also use the
Range
object to get the values from a specific range in your worksheet and use them in the email body:Dim rng As Range Set rng = Range("A1:A10") Dim emailBody As String emailBody = "The values are: " & Join(Application.Transpose(rng.Value), ", ")
- If you want to send an email with a specific format, you can use the
Format
property:Dim olFormat As Object Set olFormat = olMail.BodyFormat olFormat = olFormat.olFormatHTML
Remember to replace the placeholders (e.g., "[email protected]", "Test Email from Excel", "This is a test email sent from Excel.") with your actual email address, subject line, and email body.