Skip to main content

Sending Mail Automatically via Excel VBA

Hi one and all,
This time I have come up with a code to send emails automatically.
This code may have plenty of uses to many of you. Actually it uses Outlook email client to send email.
The example use can be if you run a procedure, you may want to intimate yourself or other via email, at the end of the Procedure, that task as been accomplished.
Make sure OUTLOOK Client application is open before running this macro.
Here is the code ( with Late Binding )

       
Sub SendMailAuto()
    Set OL_Obj = CreateObject("Outlook.Application")
    Set New_Mail = OL_Obj.CreateItem(olMailItem)
        
    With New_Mail
            .To = "Add Second Recipient Here"
            .BCC = "Add First Recipient Here"
            .CC = "Add Third Recipient Here"
            .Attachments.Add "Path of the file or Document"
            .Subject = "Enter your Subject here."
            .Body = "Write the Content of the Mail"
            .Display
    End With
    For i = 1 To 1000: DoEvents: Next   ' Putting a halt to allow Outlook window to show up.
    SendKeys "%s", True
End Sub



If you want to try Early binding, you have to first refer to Microsoft Outlook xx.x Object library form Tools menu-------> References. Here is the Code:-

       
Sub SendMailAuto()
    Dim OL_Obj As Outlook.Application
    Dim New_Mail As Outlook.MailItem
    Set OL_Obj = New Outlook.Application
    Set New_Mail = OL_Obj.CreateItem(olMailItem)
    
    With New_Mail
        .To = "Add Second Recipient Here"
        .BCC = "Add First Recipient Here"
        .CC = "Add Third Recipient Here"
        .Attachments.Add "Path of the file or Document"
        .Subject = "Enter your Subject here."
        .Body = "Write the Content of the Mail"
        .Display
    End With
    
    For i = 1 To 1000: DoEvents: Next  ' Putting a halt to allow Outlook window to show up.
    SendKeys "%s", True
End Sub

Change the To, BCC, CC, Subject, Body. Attachment properties accordingly before running the macro. You can remove the unwanted properties as well.

Regards,

Comments

Popular posts from this blog

Automation:- Sending Invitation to Meeting Using Excel VBA

Hello Everyone, In one of the previous post, I wrote about automating Sending Emails using Excel VBA. This time I have come up with a pretty similar code. The code below sends Outlook Meeting Invitations to recipients on one click. here is the Code:- Sub Send_Invite_Auto() Dim olApp As Outlook.Application Dim olApt As AppointmentItem Set olApp = New Outlook.Application 'Creating Outlook Session Set olApt = olApp.CreateItem(olAppointmentItem) 'Creating an Appointment With olApt .Subject = "Enter the subject here." 'Subject .Start = DateAdd("d", 5, Now) 'Enter Date + Time here. .Recipients.Add ("example@gmail.com") 'Recipient Name, Alias, or any other Attribute. .MeetingStatus = olMeeting 'olAppointmentItem with Meeting status olMeeting 'becom

Highlighting Duplicates across multiple sheets

Hi One and All, This time I have come up with some conditional formatting stuff. The aim is to highlight the duplicates across multiple sheets and with in the sheet as well. Assumptions:- 1. I am assuming that sheet names goes on like sheet1, sheet2, sheet3.... 2. The Target Column No. is same in all the sheets. I mean, as in the attached example, its Column A which is being targeted in both sheets.

Making Password Protected PDFs using Excel Vba and PDFtk Tool

Hi Everyone, This time, I have come up a VBA Code to generate Password protected PDFs using Excel. Actually, Excel Vba has .ExportAsFixedFormat Method to generate PDFs but this hasn't any Parameter which takes password to protect the PDFs. So I have used PDFtk Tool which provide Command Line Interface to make PDFs protected using Password. Actually, You can do various things using PDFtk Tool command line varying from creating, merging, Protecting and many other. So Download the PDFtk Tool from the following link:- Download PDFtk Tookit Here are the Steps:- 1. Install the PDFtk Toolkit. 2. Use the following code to Print or Export the Activesheet with a password.