我有一个简单的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
以此类推...
我认为我应该在前两行中进行一些更改,以便它将所有选定的电子邮件(而不只是一封电子邮件)然后放入一些循环,但是我不知道如何将多选内容包括到脚本中。
答案 0 :(得分:0)
似乎您只需要遍历Outlook中的所有选定项目。 Selection
类的Explorer
属性返回一个Selection对象,该对象包含在资源管理器窗口中选择的一个或多个项目。例如,以下是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
答案 1 :(得分:0)
谢谢!我知道了,应该怎么做。
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