[英]Selecting multiple mails in Outlook for Python script
我有一個簡單的Python 2.7腳本,該腳本從Outlook中接收選定的電子郵件,然后從中提取一些信息並執行相應的操作。 我想改善自己的腳本,使其可以一次處理多封電子郵件。 我的腳本需要認識到已選擇了1封以上的電子郵件,然后再處理其中的每封電子郵件。
outlook = win32com.client.Dispatch('Outlook.Application')
selection = outlook.ActiveExplorer().Selection
num_items = selection.Count
subject = selection.Item(1).Subject
domain = selection.Item(1).SenderEmailAddress
等等...
我相信我應該在前兩行中進行一些更改,以便它將所有選定的電子郵件(而不只是一封電子郵件)然后放入一些循環,但是我不知道如何在腳本中包含此多選。
看來您只需要遍歷Outlook中的所有選定項目。 Explorer
類的Selection
屬性返回一個Selection對象,其中包含在Explorer
窗口中選擇的一個或多個項目。 例如,下面是VBA代碼,它循環訪問Outlook中選擇的所有項目:
Sub GetSelectedItems()
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim mySender As Outlook.AddressEntry
Dim oMail As Outlook.MailItem
Dim oAppt As Outlook.AppointmentItem
Dim oPA As Outlook.PropertyAccessor
Dim strSenderID As String
Const PR_SENT_REPRESENTING_ENTRYID As String = _
"http://schemas.microsoft.com/mapi/proptag/0x00410102"
Dim MsgTxt As String
Dim x As Long
MsgTxt = "Senders of selected items:"
Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
For x = 1 To myOlSel.Count
If myOlSel.Item(x).Class = OlObjectClass.olMail Then
' For mail item, use the SenderName property.
Set oMail = myOlSel.Item(x)
MsgTxt = MsgTxt & oMail.SenderName & ";"
ElseIf myOlSel.Item(x).Class = OlObjectClass.olAppointment Then
' For appointment item, use the Organizer property.
Set oAppt = myOlSel.Item(x)
MsgTxt = MsgTxt & oAppt.Organizer & ";"
Else
' For other items, use the property accessor to get the sender ID,
' then get the address entry to display the sender name.
Set oPA = myOlSel.Item(x).PropertyAccessor
strSenderID = oPA.GetProperty(PR_SENT_REPRESENTING_ENTRYID)
Set mySender = Application.Session.GetAddressEntryFromID(strSenderID)
MsgTxt = MsgTxt & mySender.Name & ";"
End If
Next x
Debug.Print MsgTxt
End Sub
謝謝! 我知道,應該怎么做。
outlook = win32com.client.Dispatch('Outlook.Application')
selection = outlook.ActiveExplorer().Selection
num_items = selection.Count
i = 1
for x in selection:
subject = selection.Item(i).Subject
domain = selection.Item(i).SenderEmailAddress
#do more staff here
i = i + 1
if i <= num_items:
continue
else:
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.