Outlook object in VBA Excel
The excel VBA can be used to do advanced tasks such as automation of Emails through Outlook program.
Most of the company data are in excel format.So, a program to automate the sharing of these data through Email will be of much use.
The VBA can be used to send the whole Workbook, a Worksheet or part of its data or any other file from the drive.
The VBA is best suited with Outlook application only, though mail automation can be done with other applications such as Gmail,Yahoo, it involves much complexity.
Create Outlook Object
To Use Outlook application, a reference has to be added to it through "Add Reference"
After that, an instance of the Outlook application has to be created as shown below:
- Dim outlook_app As Object
- Set outlook_app=CreateObject("Outlook.Application")
Now that an Outlook application object has been created.
The next step is to create a Mail object.
- Dim objmail As Object
- Set objmail=outlook_app.CreateItem(0)
This property is used to specify the destination email address.
To send to multiple destination email address, the email addresses has to be separated by a ;
This property is used to set the subjet of the mail.
This simply takes a string and adds it to the subject
This is used to send a copy of the mail to a recepient.
This property, as the name implies is used to set the body of the mail being sent.
This too takes a string argument and makes it the body of the mail document.
This property is used to add file attachments along with the mail.
The attachments takes a file location from the local disk and sends with the mail
The size constraints of the attachments are same as any other mail application.
As the name suggests, this property is used to send the mail to the recepients.
This does not take any arguments, though as everything is specified already.