MS Outlook VBA Interview Questions and Answers with Examples
MS Outlook VBA Interview Questions and Answers explained with Example macros. Here are the list of most frequently asked basic MS Outlook VBA interview questions. All the questions are answered with simple examples. These questions will help you to answer basic questions to deal with MS Outlook VBA Programming/Development.
Here is the following VBA procedure to create a new message using Outlook.
'Create a new message using Outlook Sub Create_Message() 'Variable Declaration Dim OLApp As New Outlook.Application Dim oMessage As MailItem 'Create objects Set OLApp = New Outlook.Application Set oMessage = OLApp.CreateItem(olMailItem) With oMessage .To = "Person_Name@domain.com" .Subject = "Test Email" .Body = "This is a test message." .Display End With Set objMail = Nothing Set objOL = Nothing End Sub
Here is the following VBA procedure to send a new message using Outlook
'Send a new message using Outlook Sub Send_Message() 'Variable Declaration Dim OLApp As New Outlook.Application Dim oMessage As MailItem 'Create objects Set OLApp = New Outlook.Application Set oMessage = OLApp.CreateItem(olMailItem) With oMessage .To = "emailID@domainname.com" .Subject = "Test Email" .Body = "This is a test message." .Send End With Set objMail = Nothing Set objOL = Nothing End Sub
Here is the following VBA procedure to add attachment to a message using Outlook.
'Add attachment to a message using Outlook Sub Add_Attachment_Message() 'Variable Declaration Dim oMessage As Outlook.MailItem Dim oAttachment As Outlook.Attachments 'Create Objects Set oMessage = Application.CreateItem(olMailItem) Set oAttachment = oMessage.Attachments 'Add attachment oAttachment.Add "D:/Test.doc", olByValue, 1, "Test" oMessage.Display End Sub
Here is the following VBA procedure to add attachment and send a message using Outlook.
'Add attachment and send a message using Outlook Sub Send_Message_With_Attachment() 'Variable Declaration Dim oMessage As Outlook.MailItem Dim oAttachment As Outlook.Attachments 'Create Objects Set oMessage = Application.CreateItem(olMailItem) Set oAttachment = oMessage.Attachments 'Add attachment oAttachment.Add "D:/Test.doc", olByValue, 1, "Test" 'Add message details With oMessage .To = "emailID@domainname.com" .Subject = "Test Email" .Body = "This is a test message." .Send End With End Sub
Here is the following VBA procedure to create a Task in outlook.
Create a Task Sub Create_Task() 'Variable Declaration Dim Task As TaskItem 'Create object Set myTask = Application.CreateItem(ItemType:=olTaskItem) End Sub
Here is the following VBA procedure to delete completed tasks from a folder.
'Delete completed tasks from a folder Sub Delete_Completed_Task() 'Variable Declaration Dim oTask As Outlook.TaskItem Dim oFolder As Outlook.Folder 'create object Set oFolder = Outlook.Session.GetDefaultFolder(olFolderTasks) 'Loop through each task in a folder For Each Task In oFolder.Items 'Create object Set oTask = Task 'Check the status of the task is completed or not If oTask.Status = olTaskComplete Then 'Delete completed task oTask.delete End If Next End Sub
Here is the following VBA procedure to create a folder in Outlook.
'Create a folder in Outlook Sub Create_Folder() Application.GetNamespace("MAPI").GetDefaultFolder(olFolderTasks) _ .Folders.Add Name:="MyFolder", Type:=olFolderTasks End Sub
Here is the following VBA procedure to delete a folder in Outlook.
'Delete a folder in Outlook Sub Delete_Folder() Application.GetNamespace("MAPI").GetDefaultFolder(olFolderTasks) _ .Folders("MyFolder").delete End Sub
Here is the following VBA procedure to create a new contact in Outlook.
'Create a new contact in Outlook Sub Add_Contact() 'Variable Declaration Dim oOutlook As Outlook.Application Dim NewContact As ContactItem 'Create Objects Set oOutlook = CreateObject("Outlook.Application") Set NewContact = oOutlook.CreateItem(olContactItem) 'New Contact Details With NewContact .FirstName = "Analysis" .LastName = "Tabs" .Email1Address = "emailID@domainname.com" .Save End With End Sub
Here is the following VBA procedure to delete an existing contact in Outlook.
'Delete an existing contact in Outlook Sub Delete_Contact() 'Variable Declaration Dim oOutlook As Outlook.Application Dim oInformation As NameSpace Dim Contact As ContactItem Dim eContacts As Items 'Create Objects Set oOutlook = CreateObject("Outlook.Application") Set oInformation = oOutlook.GetNamespace("MAPI") Set eContacts = oInformation.GetDefaultFolder(olFolderContacts).Items 'Check for contact to delete from outlook contacts For Each Contact In eContacts If Contact.Email1Address = "emailID@domainname.com" Then Contact.delete End If Next End Sub
Here is the following VBA procedure to delete Mail Items from a folder in Outlook.
'Delete Mail Items from a folder in Outlook Sub Delete_Items() 'Variable Declaration Dim oFolder As Outlook.Folder Dim Cnt As Long 'Create Object Set oFolder = Application.GetNamespace("MAPI"). _ GetDefaultFolder(olFolderDeletedItems) 'Loop through all items i For Cnt = oFolder.Items.Count To 1 Step -1 oFolder.Items(Cnt).delete Next End Sub
Here is the following VBA procedure to get all Contact details in outlook.
'Get all Contact details in outlook Sub Get_Contacts_From_Outlook() 'Variable declaration Dim oContactsFolder As Folder Dim oContact As ContactItem 'Create Object Set oContactsFolder = Session.GetDefaultFolder(olFolderContacts) 'Loop through each contact in a contactfolder For Each oContact In oContactsFolder.Items 'See contact details in immediate window Debug.Print oContact.CompanyName Next 'Display count of total contacts MsgBox ("Total contacts Found : " & oContactsFolder.Items.Count) End Sub
Here is the link for more VBA Interview Questions and Answers. These are explained for examples.
100+ VBA Interview Questions