2017-02-24 5 views
0

私のOutlook電子メールと未読のものだけを読んでいます。私が今持っているコードは:Pythonを使用してOutlook電子メールを逆順に移動する方法

import win32com.client 

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items 
message = messages.GetFirst() 
while message: 
    if message.Unread == True: 
     print (message.body) 
     message = messages.GetNext() 

しかし、これは最初の電子メールから最後の電子メールになります。未読のメールが上に表示されるため、逆の順序で行きたいと思っています。それを行う方法はありますか?

+0

でも、単にmessage = messages.GetFirst()を変更しないでください。メッセージが存在する場合はGetLast()、それと同等の機能を探します –

+2

はい、 'GetLast'と' GetPrevious'メソッドがあります。 – kindall

+0

'GetLast()'と 'GetNext()'は一緒に動作しません@OmidCompSCIと私は 'GetPrevious()'を見つけることができませんでした。ありがとう@kindall –

答えて

1

私は、forループがすべてを通過するのに適しているということに同意します。最後に受信した電子メールから開始することが重要な場合(特定の注文や通過する電子メールの数を制限するなど)、機能を使用してReceived Timeプロパティで並べ替えることができます。

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items 
#the Sort function will sort your messages by their ReceivedTime property, from the most recently received to the oldest. 
#If you use False instead of True, it will sort in the opposite direction: ascending order, from the oldest to the most recent. 
messages.Sort("[ReceivedTime]", True) 

for message in messages: 
    if message.Unread == True: 
     print (message.body) 
0

なぜforループを使用しないのですか?あなたのメッセージを最初から最後まで行くには、あなたがしようとしているように見えます。

for message in messages: 
    if message.Unread == True: 
     print (message.body) 
関連する問題