{"id":12109,"date":"2011-12-17T17:24:01","date_gmt":"2011-12-17T17:24:01","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1072"},"modified":"2022-08-12T11:29:13","modified_gmt":"2022-08-12T11:29:13","slug":"how-to-send-email-by-excel-macro-from-outlook","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2011\/12\/how-to-send-email-by-excel-macro-from-outlook\/","title":{"rendered":"How to send email from Excel Macro from Outlook"},"content":{"rendered":"
This is continuation of my Previous Article you learnt How to Send Email From Excel VBA<\/strong>, using Gmail and Yahoo Email Account. Adding Outlook Reference<\/p><\/div>\n
\n
\nIn my Previous Article, How to send email from Excel Macro<\/a>,<\/strong> 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.
\nBefore we get in to the code details, we need to add Microsoft Outlook 12.0 Object Librar<\/strong> reference in your Excel.
\nIf you have not added this Reference then you need to create Outlook Objects run-time.<\/p>\n<\/a>
\n\nSub SendEmailUsingOutlook()\n\nDim OlApp As New Outlook.Application\nDim myNameSp As Outlook.Namespace\nDim myInbox As Outlook.MAPIFolder\nDim myExplorer As Outlook.Explorer\nDim NewMail As Outlook.MailItem\nDim OutOpen As Boolean\n\n ' Check to see if there's an explorer window open\n ' If not then open up a new one\n OutOpen = True\n Set myExplorer = OlApp.ActiveExplorer\n If TypeName(myExplorer) = \"Nothing\" Then\n OutOpen = False\n Set myNameSp = OlApp.GetNamespace(\"MAPI\")\n Set myInbox = myNameSp.GetDefaultFolder(olFolderInbox)\n Set myExplorer = myInbox.GetExplorer\n End If\n\n ' If you don't to display your outlook while sending email then comment the below statement\n 'otherwise you can un-comment\n\n 'myExplorer.Display \n\n ' Create a new mail message item.\n Set NewMail = OlApp.CreateItem(olMailItem)\n With NewMail\n '.Display ' You don't have to show the e-mail to send it\n .Display\n .Subject = \"Happy New Year\"\n .To = \"abc@email.com\"\n .Body = \"Wishing you happy New Year\"\n .Attachments.Add (\"C:\\log.txt\")\n End With\n\n NewMail.Send\n If Not OutOpen Then OlApp.Quit\n\n 'Release memory.\n Set OlApp = Nothing\n Set myNameSp = Nothing\n Set myInbox = Nothing\n Set myExplorer = Nothing\n Set NewMail = Nothing\n\nEnd Sub\n\n<\/code><\/pre>\n