QTP - 24 Working with MS Outlook 与outlook交互
2012-02-08 14:34
423 查看
24 Working with MS Outlook
Concept: Outlook operation can be automated using COM.24.0 Outlook Object model
Application --Namespace **AddressLists **AddressEntries **Folders **MAPIFolder **Items * Actoins * Attachments * UserProperties * RecurrencePattern * Recipients --Assistant --Explorer --Inspector **Page |
“Folders”– collection of folders under the namespace (Mail folders, Public Folders, PST)
“Items” –collection of outlook items (contacts, emails, calendar, appointment etc)
“MAPIFolder” – sub folders present inside the MAPI folders
24.1 Launching Outlook Application
Set olApp=CreateObject("Outlook.Application") Set olMAPI=olApp.GetNamespace("MAPI") |
24.2 Logging in to Namespace
olMAPI.Logon(Profile, password, showDialog, NewSession) |
olMAPI.Logon ,,False , True olMAPI.Logoff |
24.3 Enumerating the top level folder
思路:主要利用Folder的方法和属性For Each topFolder in olMAPI.Folders print ""& (topFolder.Name) Next |
*Access to specific folder Set oFirstFolder = olMAPI.Folders.GetFirst Set oFirstFolder = olMAPI.Folders.Item(1) Set oFirstFolder = olMAPI.Folders.Item("Public Folders") |
*Get parent folder Set oFirstFolderParent = oFirstFolderChild.Parent (Note: 如果oFirstFolderChild已经是top folder,就会报错。所以要error handling) |
*Get specific child folder Set oFirstFolderChild = oFirstFolder.Folders("Inbox") |
*Get folder name print oFirstFolderChild.Name |
*Get folder path print oFirstFolderChild.FolderPath |
*GetDefaultFolder Set oFolder = olMAPI.GetDefaultFolder(olFolderInbox) 'Constants for default folders Const olFolderDeletedItems = 3 Const olFolderOutbox = 4 Const olFolderSentMail = 5 Const olFolderInbox = 6 Const olFolderCalendar = 9 Const olFolderContacts = 10 Const olFolderJournal = 11 Const olFolderNotes = 12 Const olFolderTasks = 13 Const olFolderDrafts = 16 Const olPublicFoldersAllPublicFolders = 18 Const olFolderConflicts = 19 Const olFolderSyncIssues = 20 Const olFolderLocalFailures = 21 Const olFolderServerFailures = 22 Const olFolderJunk = 23 |
*Get folder’s emails Set allEmails = oFolder.Items |
24.4 Downloading attachment from a specific email
Set oFolder = olMAPI.GetDefaultFolder(6) Set email = oFolder.items(1) ‘按邮件时间排序,最早邮件为1. For Each attachment In email.Attachments attachment.SaveAsFile "C:\" & attachment.FileName Next |
23.5 Filter the emails: Restrict Method
You cancheck the email if be read:If email.Unread = True Then |
Set allEmails = oFolder.Items Set unreadEmails = allEmails.Restrict("[CC]='' And Not [Unread] = True") print unreadEmails.Count |
Property | Description |
Attachments | Collection of attachments in the mail |
CC | CC of the mail |
ConversationIndex | Index of the mail in conversation |
ConversationTopic | Subject of the conversion(without RE: FW: etc) |
HTMLBody | HTML text of the body |
ItemProperties | Collection of all email properties |
Parent | Get parent folder |
Recipients | Collection of all recipient |
SenderEmailAddress | Email Address of the sender |
SenderEmailType | Sender email type. E.g. SMTP |
SenderName | Name of the Sender |
Size | Size of the mail |
Subject | Subject of the email address |
To | To list |
UnRead | Tells if email is unread or not |
Body BodyFormat | Text of the email Format of the email(HTML, Plain text) |
24.6 Sending an E-mail Message
Mail Types:Const olMailItem = 0 Const olAppointmentItem = 1 Const olContactItem = 2 Const olTaskItem = 3 Const olJournalItem = 4 Const olNoteItem = 5 Const olPostItem = 6 Const olDistributionListItem = 7 |
Set newMail = olApp.CreateItem(olMailItem) 'Populate one or more of the email address fields newMail.To = "xing-xing.tan@hp.com" newMail.CC = "xing-xing.tan@hp.com" newMail.BCC = "xing-xing.tan@hp.com" 'Edit in the message's subject and body information newMail.Subject = "Test Subject By QTP" newMail.Body = "Body of the email" |
24.7 Outlook Security Dialogs:
Method1: Send the email through Outlook UI:'Create a description Set oMailWindow = Description.Create oMailWindow("title").Value = emailCaption oMailWindow("title").RegularExpression = False sTitle = "title:=Microsoft Office Outlook" 'Check if a spelling cancelled dialog is present If Window(oMailWindow).Window(sTitle).Exist(2) Then Window(oMailWindow).Window(sTitle).WinButton("text:=&Yes").Click End If |
Method3: Use “Working with Modal Dialog boxes” as discussedin the “Working with APIs” chapter
24.8 Getting address from email message:
For Each oRecip In item.Recipients 'Is the recipient in TO list If oRecip.Type = olTo Then sTo = sTo & oRecip.Address & ";" 'Is the recipient in CC list ElseIf oRecip.Type = olCC Then sCC = sCC & oRecip.Address End If Next |
24.9 Clicking links inside an email message:
思考:Outlooksupport 3 formats: Plain text, RTF, html. But Outlook can get HTMLcontent no matter which format displayed. Save HTML context into IE browser,then can be done.Set oFolder = olMAPI.GetDefaultFolder(6) Set email = oFolder.Items(1) HTMLText = email.HTMLBody SystemUtil.Run "iexplore.exe" Wait 5 Browser("creationtime:=0").object.document.write HTMLText Set allLinks = Browser("creationtime:=0").object.document.links |
24.10 Launching QTP using an email trigger
不做研究。相关文章推荐
- QTP -25 Working with MS Word 与word交互
- QTP - 23 (Working with MS Excel) QTP与Excel交互
- QTP - 26 Working with MS IE browser 与IE 交互
- QTP-Working with Outlook-ClickYes Pro
- QTP - 17 Working with APIs 与windows API交互
- QTP-21 Working with XML 与XML交互
- QTP -20 Working with web Tables 与webTable交互
- QTP - 27 Working with HP’s QC 与QC交互
- QTP-22 Working with Databases 与DB交互
- Manual:Core:Working with JSON (EXT 与json的交互)
- Working with MS Excel(xls / xlsx) Using MDAC and Oledb
- QTP -30 Working with .NET Classes
- 与android database 交互(chapter 14-Working with the SQLite Database)
- Working with Preferences and Saving State
- Windows Phone 8 SDK on a Virtual Machine with Working Emulator
- (转)C#与Outlook交互收发邮件
- How to communicate with CDC and MS composite device
- Working with the Eclipse FileSystem
- Working with iPhone files and folders
- Working with Variants in Visual Basic