Outlook search from QTP

Question ID: 104985

Is there a way in QTP to search the inbox of Outlook for a specific e-mail? Once found, is there a way to view the

Marked as spam
Posted by (Questions: 175, Answers: 22)
Asked on April 30, 2013 3:31 pm
Answers (1)
Private answer

You can use Outlook`s Automation Object Model to search for e-mails and select attachments

The example code below demonstrates how to search for the first e-mail from ''test@tester.com.'' If the
e-mail has attachments, they will be saved to a local drive and then opened for viewing.

This code is not part of QuickTest Professional and is not guaranteed to work with all versions of Outlook.

Set WshShell = CreateObject(''WScript.Shell'')
Set objOutlook = CreateObject(''Outlook.Application'') ' Create an Outlook object to interact with.
Set myNameSpace = objOutlook.GetNameSpace(''MAPI'')
Set ClientFolder = myNameSpace.GetDefaultFolder(6) ' 6 is for Inbox
Set myItems = ClientFolder.Items
Set myItem = myItems.GetFirst

For i=1 to ClientFolder.Items.Count
If (myItem.SenderName = ''test@tester.com'') Then 'Check for Sender
MsgBox myItem.Subject,,''Email Subject'' 'Displays the Subject
MsgBox myItem.Body,,''Email Content'' 'Displays the Body of the mail

If myItem.Attachments.Count <> 0 Then
attachments = ''FileName'' & vbTab & vbTab & ''Type'' & vbCr & vbCr
For j = 1 To myItem.Attachments.Count
FileName = myItem.Attachments.Item(j)
FileType = Split(FileName,''.'')
attachments = attachments & FileName & vbTab & vbTab & FileType(1) & vbCr
myItem.Attachments.Item(j).SaveAsFile ''c:'' & FileName 'Saving the Attachments
WshShell.Run ''c:'' & FileName 'Opening the Attachments
MsgBox attachments,,''Attachment Status'' 'Will display Attachment details
MsgBox ''No Attachments with this Email'',,''Attachment Status''
End If

Exit For 'Remove this Statement, if you want to look for all matching emails.
End IF

Set myItem = myItems.GetNext


Marked as spam
Posted by (Questions: 2, Answers: 425)
Answered on April 30, 2013 3:32 pm