Part 5: Automating Outlook using QTP | Emails in Outlook
In this article, you would see how to access emails in outlook, how to send emails using outlook and how to access different properties of an email item. To access email items, you can use MailItem class which contains the methods and properties to work with email items. Let’s directly jump onto the examples –
Sample Code 1: Sending emails using Outlook. This topic has already been covered in a previous article. You can read how to send emails using Outlook (You can also read how to send emails using GMail and Yahoo).
Sample Code 2: Access different properties of a mail item. In this example, you’ll see how to access the mails in an outlook folder (eg. inbox folder) and find out various properties of a particular mail item.
Dim olFolderInbox, iTotalMails, sSubject olFolderInbox = 6 : sSubject = "" Set objOutlook = CreateObject("Outlook.Application") Set objNamespace = objOutlook.GetNamespace("MAPI") 'Create reference to Inbox Folder Set oInbox = objNamespace.GetDefaultFolder(olFolderInbox) 'Find all items in the Inbox Folder Set oAllMails = oInbox.Items 'Find out properties of the mail item sSubject = sSubject & "To -> " & oAllMails(1).To & vbCrLf sSubject = sSubject & "CC -> " & oAllMails(1).CC & vbCrLf sSubject = sSubject & "BCC -> " & oAllMails(1).BCC & vbCrLf sSubject = sSubject & "Subject -> " & oAllMails(1).Subject & vbCrLf sSubject = sSubject & "Body -> " & oAllMails(1).Body & vbCrLf sSubject = sSubject & "Creation Time -> " & oAllMails(1).CreationTime & vbCrLf sSubject = sSubject & "Is Marked Important -> " & oAllMails(1).Importance & vbCrLf sSubject = sSubject & "Received at -> " & oAllMails(1).ReceivedTime & vbCrLf 'Display the result msgbox sSubject
Sample Code 3: Find out the list of all unread emails in Outlook Inbox. This example loops through all the emails in Inbox folder and reports out the unread email items.
Dim olFolderInbox, iTotalMails, sSubject olFolderInbox = 6 : sSubject = "" Set objOutlook = CreateObject("Outlook.Application") Set objNamespace = objOutlook.GetNamespace("MAPI") 'Create reference to Inbox Folder Set oInbox = objNamespace.GetDefaultFolder(olFolderInbox) 'Find all items in the Inbox Folder Set oAllMails = oInbox.Items iTotalMails = oAllMails.Count 'Loop through the mail items For i=1 to iTotalMails 'Check if the mail is UnRead or not If oAllMails(i).UnRead = True Then sSubject = sSubject & oAllMails(i).Subject & vbCrLf End If Next 'Display the results msgbox sSubject
This article just lists few of the important functions of outlook mail items. You can refer Outlook Object Model to find out more about the different properties and methods that can be used with outlook mail items.
If you feel that we have missed out any important concept, or if you want any other topic to be covered here, please leave a comment or just drop in a mail to us. We’ll be more than happy to work on it.
If you enjoyed this article, you can join our blog to get new posts delivered directly in your inbox.