Friday, 14 February 2014

Working with MS Outlook application using QTP script

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 like below.

Set objOutlook = CreateObject("Outlook.Application")

*******************************************************************************

GetNameSpace() Method :

-> Returns a NameSpace object of the specified type.
-> The Namespace object is multiple objects. The object itself provides methods for logging in and out, accessing storage objects directly by ID, accessing certain special default folders directly, and accessing data sources owned by other users.


Syntax : ApplicationObject.GetNameSpace(Type)


Type  is  Required String. The type of name space to return.


Example :

ApplicationObject.GetNameSpace ("MAPI") -  To return the Outlook NameSpace object from the Application object.


The only data source supported is MAPI, which allows access to all Outlook data stored in the user's mail stores.GetNamespace method used to bind to the MAPI namespace.


*******************************************************************************


MAPIFolder object


The MAPIFolder object represents a folder that contains email messages, tasks, contacts and various other items. MS Outlook provides a total of 16 default MAPIFolder objects.

 Example : olFolderInbox represents the Inbox folder. Similarly olFolderOutbox, olFolderSentMail, olFolderDeletedItems represent Outbox, Sent Mail & Deleted Items folders respectively.

MAPIFolder provides 16 default objects using which you can access different Outlook folders such as e-mail messages, tasks, contact items etc.

***********************************************************************************

16 Defult outlook folder type

Name
Value
Description
olFolderCalendar 9 The Calendar folder.
olFolderConflicts 19 The Conflicts folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderContacts 10 The Contacts folder.
olFolderDeletedItems 3 The Deleted Items folder.
olFolderDrafts 16 The Drafts folder.
olFolderInbox 6 The Inbox folder.
olFolderJournal 11 The Journal folder.
olFolderJunk 23 The Junk E-Mail folder.
olFolderLocalFailures 21 The Local Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderManagedEmail 29 The top-level folder in the Managed Folders group. For more information on Managed Folders, see Help in Microsoft Outlook. Only available for an Exchange account.
olFolderNotes 12 The Notes folder.
olFolderOutbox 4 The Outbox folder.
olFolderSentMail 5 The Sent Mail folder.
olFolderServerFailures 22 The Server Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderSyncIssues 20 The Sync Issues folder. Only available for an Exchange account.
olFolderTasks 13 The Tasks folder.
olFolderToDo 28 The To Do folder.
olPublicFoldersAllPublicFolders 18 The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account.
olFolderRssFeeds 25 The RSS Feeds folder.



***********************************************************************************

GetDefaultFolder Method

Returns a MAPIFolder object that represents the default folder of the requested type for the current profile.

Syntax :

object.GetDefaultFolder(Request folder number)



***********************************************************************************



How to read first top most Mail item from outlook using QTP script


Dim InboxFolder=6    // Default index for inbox folder is 6.

Set ObjOUtlook = CreateObject("Outlook.Application")      // Create object for outlook application
Set objNamespace = objOutlook.GetNamespace("MAPI")       // Name Space Returns the Application object. "MAPI" is the only supported name space type.


Set oInbox = objNamespace.GetDefaultFolder(InboxFolder)        // Create default index object for inbox folder.

Set oAllMails = oInbox.Items                 //Find all Mail items in the Inbox Folder

'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


***********************************************************************************

Display total number of folders and name of each folder inside Inbox Folder from OUtlook using QTP script


Dim InboxFolder=6
Set ObjOUtlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI") 
Set oInbox = objNamespace.GetDefaultFolder(InboxFolder)

Set oAllFolders = oInbox.Count                    // Find how many folders inside inbox.

sFolderNames = "Total Folders = " & iCount & vbcrlf & vbcrlf    // Get the name of folders


For i = 1 to iCount
    sFolderNames = sFolderNames & "Folder " & i & " -> " & oAllFolders(i).Name & vbcrlf
Next

msgbox sFolderNames                          //Display folder count and folder names in msgbox


**********************************************************************************

Find out the list of all unread emails in Outlook Inbox


Dim InboxFolder=6
Set ObjOUtlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI") 
Set oInbox = objNamespace.GetDefaultFolder(InboxFolder)
Set oAllMails = oInbox.Items 


iTotalMails = oAllMails.Count        // Count the total NUmber of Read and Unread Mail in inbox folder.           

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

msgbox sSubject

*******************************************************************************

Refer this link for more property of Mail Item -> http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mailitem_properties.aspx
 

No comments:

Post a Comment