Friday, June 17, 2011

QTP and Outlook

Here are some useful QTP functions for working with Outlook:

' #1 ========================== Send email with attachment from Outlook ======================
Public Function SendEmail(ByRef receiver, ByRef subject, ByRef body, ByRef attachment)
    Dim objOutlookMsg,objOutlook
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    objOutlookMsg.To = receiver
    objOutlookMsg.CC = ""
    objOutlookMsg.BCC = ""
    objOutlookMsg.Subject = subject
    objOutlookMsg.Body = body
    objOutlookMsg.Attachments.Add attachment
    objOutlookMsg.Send
End Function

' #2 ========================== Read email from Outlook ==================================
 Public Function ReadEmail(ByRef Subject)
   Dim objOutlookRead,objOutlook,objFolder
    'Subject="this is the message body"
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookRead = objOutlook.GetNamespace("MAPI")
    Set objFolder=objOutlookRead.GetDefaultFolder(6)
    For each item1 in objFolder.Items
        If item1.unread  Then
'            item1.unread = false
            If InStr(item1.body, Subject) then
                 ReadEmail=True
            else
                ReadEmail=false
            End if
        End If
    Next
    wait 2
End Function

' #3 ========================== Delete email from Outlook =================================
Public function DeleteEmail(ByRef Subject)
   Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookRead = objOutlook.GetNamespace("MAPI")
    Set objFolder=objOutlookRead.GetDefaultFolder(6)
    For each item1 in objFolder.Items
        If InStr(item1.body,Subject) then
            item1.delete
        End If
    Next
    wait 2
End Function

' #4 ========================== Download Email Attachment from Outlook ======================
Public Function DownloadAttachment
      Dim objOutlookRead,objOutlook,objFolder
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookRead = objOutlook.GetNamespace("MAPI")
    Set objFolder=objOutlookRead.GetDefaultFolder(6)
    For each item1 in objFolder.Items
        If item1.unread  Then
            For each att in item1.attachments
                FileName="C:\Documents and Settings\administrator\Desktop\"&att.FileName&"1"
                att.saveasfile FileName
            Next
        item1.unread = false
        End If
    Next
    wait 5
End Function

' #5 ========================== Check Email Attachment exists in Outlook =====================
Public Function CheckForAttachment(ByRef attachment)
    Dim objOutlookRead,objOutlook,objFolder
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookRead = objOutlook.GetNamespace("MAPI")
    Set objFolder=objOutlookRead.GetDefaultFolder(6)
    For each item1 in objFolder.Items
        If item1.unread  Then
            For each att in item1.attachments
                FileName=att.FileName
                If InStr(FileName, attachment) then
                 CheckForAttachment=True
                else
                 CheckForAttachment=false
                End if
            Next
        item1.unread = false
        End If
    Next
    wait 5
End Function

To be continued...

No comments:

Post a Comment