• Home  / 
  • VBA
  •  /  Get Rid of Mail Merge Once and for All

Get Rid of Mail Merge Once and for All

In this blog, I will try to cover how to get rid of Mail Merge by using the sample Excel VBA code. We all know how to do the mail merge in MS Word. But today I will discuss on mail merge in Excel. The data in your spreadsheet and you want to send the mail to the multiple people at a time.

For example, in your spreadsheet, you have the data like Firstname, Lastname, Email-id, date of Birth and phone numbers. You have a long list like this. Now every day you need to check the date of birth and send the email to the associated person.

Now I can show you how to write a simple VBA code that will check the date of birth and then send the email using either email or from your outlook.

Let’s assume you have the sample the data like below in a sheet called “DataSheet”Get rid of mail merge

(Picture 1)

It’s simple data set of the employee. We need to check whose birth date is today and then you need to send an email.
Now I will show you how to write a simple that will check the birthday of the employees and sent them email form your outlook or Gmail taking the email id from the sheet.

  1. We will create a simple template for out later reference. I have added a new sheet and changed the name of the sheet as “Mail Merge”.
  1. Then I have created a simple template like below.

Get rid of mail merge

(Picture 2)

I have created this template in Excel using page layout. I have inserted a picture in the corner which is downloaded from the internet. In future, if you want to take the print out of the birthday boys it will work.

Now you need to write the code. Press Alt +F11 to go to VBA page. Then Click on insert and then click on Module.

Get rid of mail merge(Picture 3)

After creating the new module copy the below code and then paste the below code in the module. We will catch the code one by one line later. This code is applicable when you are sending from your Gmail id.

Interesting posts on Excel VBA: Merge workbooks into single spreadsheet

Before pasting the code in the module, make sure that Microsoft CDO for windows 2000 library check box is on. To check the same just follow the below steps.

  1. Click on Tools
  2. Then go to References
  3. Microsoft CDO for windows 2000 library

Get rid of mail merge(Picture 4)

If this is not checked then please ticked the box and then press OK.
Then you need to paste the below code in the module.

You need to change the references as per your data. In my case date of birth is in D column and email id is present in G Column. And in H column I have checked where the date of birth is today or not.

You might also like to read about this: Learn more about Tricks of Text to Columns in MS Excel

Now go back to MailMerge sheet and then insert a shape. And then right click on it and then click on assign macros and select the name the procedure you have pasted just now.

Get rid of mail merge(Picture 5)

Now if you click on the shape then it will call your Sendmail procedure and it will send email from your Gmail account. But before that you need to give a valid user name and your gmail password.

If your less secure app is off in your gmail account then probably you will not able to deliver the email. So before hit the button go back to your gmail account and set less secure app is on.

Get rid of mail merge(Picture 6)

Code for using Outlook

This code will help you to send the email from outlook.  Download the files from Here.
Further, let me know if you created a variant for this? Happy to discuss new ideas 🙂


About the author

Rishabh Pugalia and Niladri Sekhar Biswas

Author CA. Rishabh Pugalia has 10+ yrs. of work experience in Audit, Interest Rates Trading & I-Banking & Author Niladri Sekhar Biswas has 9 years of experience working in Microsoft Excel, as Excel and VBA corporate trainer

  • Hussein Mohamed

    you are the best 🙂

    Thanks a lot for improving our knowledge