Part 1: Automating Outlook using QTP | General Introduction
From today, we are starting off a series of articles on how to automate Microsoft Outlook using QTP. In these articles, we’ll try to cover the important and frequently used functionalities of MS Outlook. In this first article, we will start with the basics of Outlook automation and later would move on to more specific/advanced topics.
How to begin automating MS Outlook?
Now the first question that might come to your mind is – How do you actually start with the automation of MS Outlook? Now we all know that MS Outlook is an application/software that provides many functionalities such as sending & receiving emails, managing your tasks, calender, contacts and many other things.
MS Outlook also provides a set of classes and interfaces using which you can perform the above mentioned Outlook tasks through code without directly doing it manually in Outlook. So all you have to do is write some code that will first access the classes and interfaces exposed by MS Outlook. Once this is done, you would have access to all the methods and properties using which you can perform various Outlook tasks such as sending mails, creating new folders, distribution lists etc. This set of classes & interfaces is collectively called the Outlook Object Model.
Let us now create a sample script in QTP that would access the Outlook Object Model and use it to open Outlook and display the Inbox folder. In the Outlook Object Model, ‘Application’ Object is the topmost or parent Object that represents the entire Outlook Application. So we’ll first create an instance of Outlook.Application object.
Set objOutlook = CreateObject("Outlook.Application")
Once this is done, we’ll then use GetNamespace method to bind to the MAPI namespace.
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Next, we have to create a reference to the inbox folder and then display it. This can be done using the below lines of code –
Const olFolderInbox = 6 'Inbox folder in Outlook is referenced through value 6 Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox) objFolder.Display
You can also play around with other properties and methods of the Application object. [To view the entire list of properties and methods available with the application object, you can refer the Outlook Object Model Documentation from MSDN Library.]
'Check the total number of reminders available Set objAllReminders = objOutlook.Reminders msgbox objAllReminders.Count 'Check the version of the Outlook installed msgbox objOutlook.Version 'Check the Product Code - Microsoft Outlook globally unique identifier (GUID). msgbox objOutlook.ProductCode 'Close MS Outlook objOutlook.Quit()
To make the code more robust, you can first check if outlook is already open or not, and open Outlook only if it is not already open. Let’s see the full code for the same.
Sample Code 1: How to Open & Close Outlook using QTP
'Check if outlook is already open strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set objTaskMgrProc = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'outlook.exe'") 'If outlook is not open, then open it If objTaskMgrProc.Count = 0 Then Set objOutlook = CreateObject("Outlook.Application") Set objNameSpace = objOutlook.GetNamespace("MAPI") Set objFolder = objNameSpace.GetDefaultFolder(6) objFolder.Display 'Check the total number of reminders available Set objAllReminders = objOutlook.Reminders msgbox objAllReminders.Count 'Close Outlook objOutlook.Quit() Else msgbox "Outlook already open" End If
This article is just the beginning where you have seen how to open and close Outlook using QTP. In the upcoming articles, we’ll explore more specific topics in details such as –
- .. How to create & send emails, attach files to emails, retrieve unread mails from Inbox etc.
- .. How to access Outlook Contacts, add/delete contacts & search for specific contacts.
- .. How to work with Outlook folders, Calender Items, Appointments etc.