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.
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 = "firstname.lastname@example.org" .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