Friday, August 21, 2009

QTP & Microsoft Outlook Automation

Outlook Object Model and QTP & Microsoft Outlook Scripts



In Outlook Object Model, Application class is at the root.

First, Outlook requires that you create a reference to what it calls a Namespace object. This represents one of the messaging service provider layers that Outlook depends on for data storage (although “MAPI” is the only type of namespace Outlook currently supports).

MAPI (Messaging Application Programming Interface) implements persistent data storage using a hierarchical folder metaphor similar to disk subdirectories. The Outlook's Namespace class contains a Folders collection representing the top-level folder of each installed storage system. Each of these, in turn, contains a Folders collection with members for each subfolder (Inbox, Outbox, and so on). Every folder object has a Folders collection, allowing for infinite nesting of data storage.

Data in folders is represented by an Items collection. Each element of this collection can be one of a variety of object classes that represent such things as mail messages, appointments, journal entries, contacts, and tasks. It is this uncertainty about what a folder contains that makes programming with Outlook challenging. [Source]

Outlook Object Model Reference

Outlook 2003 Object Model Map

Outlook Object Model Overview

A little bit more on MAPI

To understand MAPI, you must first understand what an application programming interface is.

At the code level, a program’s functions are invoked through specific instructions. The collection of those instructions is referred to as an application programming interface (API). That phrase is appropriate because the API allows a programmer to interface with the functions of a program. For example, if a program has the ability to read a message, there is a specific API instruction, also called a function call, that can invoke that ability. If two programs need to interact, they must do so with an API they both understand. For example, if program A sends the instruction Read_Message 4 to program B, but program B understands only the instruction Message_4_Read, then the instruction will not be understood. Humans can use slightly different grammar and still understand one another, but computers are not that forgiving.

In the past, many client/server messaging products had their own APIs for the client/server interaction. If someone wrote a client program, it would work only with the messaging system whose API it used. If a user needed to connect to multiple messaging systems, multiple client programs were needed.

Microsoft decided to remedy that situation by creating a standard messaging architecture, referred to as the Messaging API (MAPI). MAPI accomplishes two broad goals. One, it provides a standard API for client/server messaging interaction. This role makes MAPI a type of middleware, meaning that it stands in the middle between clients and servers. Some authors refer to middleware as the slash (/) between the words client and server. MAPI makes it possible for a single-client application to access different messaging servers. See Figure 1.7 for an illustration. The second broad goal of MAPI is to provide a standard set of services to client messaging applications. These services include address books, message storage, and transport mechanisms.

Even when using different types of MAPI applications, such as e-mail, fax, and voicemail, a user can access a single address book (a universal address book) and store different data types in the same folder (a universal inbox). The transport mechanisms relate to a single client application that can connect to different messaging systems. A single MAPI e-mail application can access an Exchange server, a Microsoft Mail post office, an Internet mail server, and others. Although MAPI includes individual API instructions, it most often communicates.[Source]

MAPI brings certain advantages to the table. For instance, MAPI allows you to manipulate views. MAPI allows you to add custom features to the options menu in Outlook. MAPI also provides performance improvements as compared to implementations that run without MAPI. etc

The data contained in Outlook is organized into Outlook folders. MAPI provides access to the data stored in these folder structures through the Application object. [Source]

Few QTP & Microsoft Outlook Scripts

QTP Script to get inbox folder name in messagebox

QTP Script to count emails in any Outlook folder

QTP Script to enumerate all folders in Outlook

QTP Script to send an email from Outlook

QTP Script to send an email with attachment from Outlook

QTP Script to read an email from Outlook

QTP Script to download an email attachment from Outlook