site
stats
By Anish Pillai Anish Pillai Posted under Advanced Concepts | QTP Concepts

Part 5: Automating Outlook using QTP | Emails in Outlook

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

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
Outlook Email Properties

Outlook Email Properties



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
List of Unread Emails in Outlook Inbox

List of Unread Emails in Outlook Inbox

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.


0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Poonam Lamba

    It was a great help for me. As I was to Automate a customized version of MS Outlook.

    • Anish10110

      I'm really glad that the article was of help to you.. 🙂

  • Darshan

    What is the keyword to see the "FROM" email address

  • Anil

    how can I send email using secondary outlook account? It always send mails using default account only.

  • saba ustad

    How can i access the non default folders in outlook??

  • Venky

    Hi,

    I need to delete the the messages in conversation history in outlook.

    Could you please tell me, what is the constant to access conversation folder and how to read the messages in it?

    Thankyou,
    Venkatesh

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×