Outlook Automation Using VBScript: A Comprehensive Guide with Code Examples
Microsoft Outlook is a widely used email client for personal and professional communication. If you’re looking for Outlook automation for tasks like sending emails, reading emails, deleting emails, or managing contacts, you can achieve this with VBScript. VBScript provides an easy way to automate these operations using Outlook’s COM (Component Object Model) interface.
In this blog post, we will explore how to automate various operations in Outlook using VBScript, with code examples for sending, reading, and deleting emails, as well as managing contacts.
Prerequisites– outlook automation
To automate Outlook using VBScript, you need:
1. Microsoft Outlook installed: Outlook must be installed and configured on the machine.
2. VBScript environment: You can run VBScript files (.vbs) using Windows’ built-in script host.
1. Sending an Email in Outlook automation Using VBScript
Sending an email through Outlook using VBScript is one of the simplest and most common operations. Below is a script to send an email in outlook automation using VBScript
Dim OutlookApp, MailItem
Set OutlookApp = CreateObject("Outlook.Application")
Set MailItem = OutlookApp.CreateItem(0) ' 0 represents MailItem
With MailItem
.To = "recipient@example.com"
.CC = "ccrecipient@example.com"
.BCC = "bccrecipient@example.com"
.Subject = "Test Email from VBScript"
.Body = "This is an automated email sent using VBScript and Outlook."
.Attachments.Add "C:\path\to\your\file.txt" ' Optional: Attach file
.Send
End With
Set MailItem = Nothing
Set OutlookApp = Nothing
Explanation:
3 things are important in outlook automation. The object outlook.application, mail item and other methods.
• CreateObject(“Outlook.Application”): This creates an instance of the Outlook application.
• MailItem: Represents the email message object.
• You can set recipients (.To, .CC, .BCC), subject, body, and attachments.
2. Reading Emails from Inbox Using VBScript
You can read emails from the Inbox folder using VBScript and process them based on conditions like sender or subject using outlook automation
Dim OutlookApp, Namespace, Inbox, MailItem, i
Set OutlookApp = CreateObject("Outlook.Application")
Set Namespace = OutlookApp.GetNamespace("MAPI")
Set Inbox = Namespace.GetDefaultFolder(6) ' 6 represents the Inbox folder
For i = 1 To Inbox.Items.Count
Set MailItem = Inbox.Items(i)
WScript.Echo "Subject: " & MailItem.Subject
WScript.Echo "From: " & MailItem.SenderName
WScript.Echo "Received: " & MailItem.ReceivedTime
Next
Set MailItem = Nothing
Set Inbox = Nothing
Set Namespace = Nothing
Set OutlookApp = Nothing
Explanation:
• GetNamespace(“MAPI”): Retrieves the MAPI namespace.
• GetDefaultFolder(6): Refers to the Inbox folder (6 corresponds to Inbox).
• The script loops through all items in the Inbox and displays the subject, sender, and received time.
3. Deleting Emails from Inbox Using VBScript
You can delete specific emails from the Inbox based on certain criteria like subject or sender using outlook automation.
Dim OutlookApp, Namespace, Inbox, MailItem, i
Set OutlookApp = CreateObject("Outlook.Application")
Set Namespace = OutlookApp.GetNamespace("MAPI")
Set Inbox = Namespace.GetDefaultFolder(6)
For i = Inbox.Items.Count To 1 Step -1
Set MailItem = Inbox.Items(i)
If MailItem.Subject = "Test Email from VBScript" Then
MailItem.Delete
End If
Next
Set MailItem = Nothing
Set Inbox = Nothing
Set Namespace = Nothing
Set OutlookApp = Nothing
Explanation:
• This script iterates through the emails in the Inbox and deletes emails with the subject “Test Email from VBScript”.
• The loop runs backward (Step -1) to avoid conflicts with the collection when deleting items.
4. Adding a Contact in Outlook Using VBScript
You can automate the process of adding new contacts when automating Outlook using VBScript.
Dim OutlookApp, ContactsFolder, Contact
Set OutlookApp = CreateObject("Outlook.Application")
Set ContactsFolder = OutlookApp.Session.GetDefaultFolder(10) ' 10 represents Contacts folder
Set Contact = ContactsFolder.Items.Add
With Contact
.FirstName = "John"
.LastName = "Doe"
.Email1Address = "johndoe@example.com"
.Save
End With
Set Contact = Nothing
Set ContactsFolder = Nothing
Set OutlookApp = Nothing
Explanation:
• GetDefaultFolder(10): Refers to the Contacts folder.
• The script creates a new contact with the specified name and email address.
5. Deleting a Contact in Outlook Using VBScript
You can automate the deletion of contacts based on criteria such as the contact’s name.
Dim OutlookApp, ContactsFolder, Contact, i
Set OutlookApp = CreateObject("Outlook.Application")
Set ContactsFolder = OutlookApp.Session.GetDefaultFolder(10)
For i = ContactsFolder.Items.Count To 1 Step -1
Set Contact = ContactsFolder.Items(i)
If Contact.FirstName = "John" And Contact.LastName = "Doe" Then
Contact.Delete
End If
Next
Set Contact = Nothing
Set ContactsFolder = Nothing
Set OutlookApp = Nothing
Explanation:
• The script loops through all contacts and deletes the one that matches the specified first and last name.
6. Marking Emails as Read/Unread
You can automate outlook and can mark emails as read or unread using VBScript. This is useful when you want to automatically process emails and mark them accordingly in outlook automation.
Dim OutlookApp, Namespace, Inbox, MailItem, i
Set OutlookApp = CreateObject("Outlook.Application")
Set Namespace = OutlookApp.GetNamespace("MAPI")
Set Inbox = Namespace.GetDefaultFolder(6)
For i = 1 To Inbox.Items.Count
Set MailItem = Inbox.Items(i)
If MailItem.UnRead Then
MailItem.UnRead = False ' Mark as read
MailItem.Save
End If
Next
Set MailItem = Nothing
Set Inbox = Nothing
Set Namespace = Nothing
Set OutlookApp = Nothing
7. Moving Emails to a Specific Folder
You can also move emails to another folder (like “Processed” or “Archive”) using VBScript.
Dim OutlookApp, Namespace, Inbox, ProcessedFolder, MailItem, i
Set OutlookApp = CreateObject("Outlook.Application")
Set Namespace = OutlookApp.GetNamespace("MAPI")
Set Inbox = Namespace.GetDefaultFolder(6)
Set ProcessedFolder = Namespace.Folders("Mailbox").Folders("Processed")
For i = 1 To Inbox.Items.Count
Set MailItem = Inbox.Items(i)
If MailItem.Subject = "Move this email" Then
MailItem.Move ProcessedFolder
End If
Next
Set MailItem = Nothing
Set ProcessedFolder = Nothing
Set Inbox = Nothing
Set Namespace = Nothing
Set OutlookApp = Nothing
Explanation:
• The script moves an email to the “Processed” folder based on the subject.
Discover more from Automation Script
Subscribe to get the latest posts sent to your email.