How to send email from Excel Macro from Outlook

.

This is continuation of my Previous Article you learnt How to Send Email From Excel VBA, using Gmail and Yahoo Email Account.

In my Previous Article, How to send email from Excel Macro, as you saw how to send email by Excel VBA from Gmail or Yahoo In this article you are going to learn how to send an email automatically by Excel VBA from my Outlook. Here in this you need to make sure that you have Outlook installed in your system where your excel macro is running. Also for sending email your Outlook must be configured already.
Before we get in to the code details, we need to add Microsoft Outlook 12.0 Object Librar reference in your Excel.
If you have not added this Reference then you need to create Outlook Objects run-time.

Adding Outlook Reference

Adding Outlook Reference



Sub SendEmailUsingOutlook()

Dim OlApp As New Outlook.Application
Dim myNameSp As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myExplorer As Outlook.Explorer
Dim NewMail As Outlook.MailItem
Dim OutOpen As Boolean

    ' Check to see if there's an explorer window open
    ' If not then open up a new one
    OutOpen = True
    Set myExplorer = OlApp.ActiveExplorer
    If TypeName(myExplorer) = "Nothing" Then
        OutOpen = False
        Set myNameSp = OlApp.GetNamespace("MAPI")
        Set myInbox = myNameSp.GetDefaultFolder(olFolderInbox)
        Set myExplorer = myInbox.GetExplorer
    End If

    ' If you  don't to display your outlook while sending email then comment the below statement
    'otherwise you can un-comment

    'myExplorer.Display 

    ' Create a new mail message item.
    Set NewMail = OlApp.CreateItem(olMailItem)
    With NewMail
        '.Display ' You don't have to show the e-mail to send it
        .Display
        .Subject = "Happy New Year"
        .To = "abc@email.com"
        .Body = "Wishing you happy New Year"
        .Attachments.Add ("C:\log.txt")
    End With

    NewMail.Send
    If Not OutOpen Then OlApp.Quit

    'Release memory.
    Set OlApp = Nothing
    Set myNameSp = Nothing
    Set myInbox = Nothing
    Set myExplorer = Nothing
    Set NewMail = Nothing

End Sub

 
To know How to Send Email from Excel Macro using Gmail and Yahoo Account Read this Article

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

15 Comments

  1. alfred beilin

    hi happy new year to yous all

    a beilin

    Reply
    • Vishwamitra Mishra

      Thanks Alfred and same to you.

      Reply
      • Yogisha

        Hi,

        This format is very good.

        Reply
        • Vishwamitra Mishra

          Thanks Yogisha !!

          Reply
  2. Priya

    Hi Vishwa… i tried tis macro…but my mail s getting stored in outbox… and the mail is not sent. why is it so?

    Reply
    • Vishwamitra Mishra

      Hi Priya,

      Remove this statement .Display from the code and try.

      With NewMail
      '.Display ' You don't have to show the e-mail to send it
      .Display
      .Subject = "Happy New Year"
      .To = "abc@email.com"
      .Body = "Wishing you happy New Year"
      .Attachments.Add ("C:log.txt")
      End With

      Reply
  3. Kalyan

    Hi ,

    Can i have any video reference ..how does this works ..i tried but it dint work

    My requirement is i have excel where it get automatic updates from SQL Data Base …now we have to send this excel every day to Group Mails..sometimes we miss to send … with out our manual work …how to automate to send this excel sheet

    Kalyan

    Reply
  4. Riis80

    Hi

    I have ahopefully quick question.

    How do you use an email adress, from a cell? or example if I have an email in excell i cell A1, how can that be inserted?

    Jakob

    Reply
  5. ori

    how can i forces the Language to be Hebrew or make the body Alignment = right

    thanx

    ori

    Reply
  6. Pradip

    Hi Vishwamitra,

    Your code gives me the type mismatch error. can you help with this?

    I am getting this error in

    ” Set NewMail = OlApp.CreateItem(olMailItem)”

    Please help.

    THanks again for writing such a nice blog.

    Reply
  7. eshwar

    hi

    is there any macro to copy the any selected range data from excell and email

    Reply
  8. Donnie Oberhaus

    This web site does not render correctly on my droid – you may want to try and repair that

    Reply
  9. Sathish E

    Hi Vishwamitra,

    I want to know small coding to send simple email, can you help me on this,
    Email should be like,

    Hi ZZZZZZZZ,

    Could you please provide us the payment balances of XXXXXXX to be updated in Non-Netting Website for the month of Dec’14.

    Please provide us the details ASAP as we need to work with other restricted entities as well and update the same within XXXXXXXX.

    Thanks,
    Sathish E

    Reply
  10. Kavinraj

    Please tell me how to add voting buttons while sending Email from excel VBA.

    Reply
  11. Manikandan JM

    Hi,

    I have a excel macro which sends mail using Outlook. I have configured 2 email accounts (say emailid 1 & email id 2) in outlook for two specific purposes. Whenever I run the macro, all the mails are sent through default email id (say email id 1). Is it possible to send mails using emailid 2.

    Reply

Trackbacks/Pingbacks

  1. Learn Excel Macro How to Send ActiveWorkbook as attachment in Email - [...] previous Article, i had written how to Send Email from Excel Macro. In that article we had discussed how…
  2. Mail Chart as Image from Outlook - VBA Code - Welcome to LearnExcelMacro.com - […] Now send the Chart Image by either way from outlook. Read more about how to send emails from Outlook…

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest