PowerShell/VBA Outlook排序邮件并获取第一封

https://stackoverflow.com/questions/78924/when-is-a-mailitem-not-a-mailitem

Powershell

$xlOutlook = New-Object -ComObject Outlook.Application;

$NS = $xlOutlook.Session.Application.GetNamespace("MAPI");

$InboxFolder = $NS.folders("jy.tang@kpmg.com").folders("Inbox");

#$mail = $InboxFolder.items($InboxFolder.Items.Count);

$getItem = $InboxFolder.Items;

###### 这里的FASLE是最迟到最早的邮件排序,VBA刚好跟这里相反

$getItem.sort("[ReceivedTime]",$false);

$mail = $getItem[$getItem.Count];

Write-Host $mail.subject

$xlOutlook = $null;

[System.GC]::Collect();


Vba

Sub SortByDueDate()

Dim myNameSpace As Outlook.Namespace

Dim myFolder As Outlook.Folder

Dim myItem As Object

Dim myItems As Outlook.Items

Dim myMail As Outlook.MailItem

Dim xlapp As Outlook.Application

Set xlapp = New Outlook.Application

Set myNameSpace = xlapp.GetNamespace("MAPI")

Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

Set myItems = myFolder.Items

'为True时,邮件从最迟到最早排序,False为相反

myItems.Sort "[ReceivedTime]", True

For Each myItem In myItems

' 所有MAIL都是 OUTLOOK.ITEM, 但是开会的MAIL是53,而不开会的MAIL是43

'If myItem.Class = 43 Then

If TypeOf myItem Is MailItem Then

Set myMail = myItem

'Debug.Print myMail.Subject

ElseIf TypeOf myItem Is MeetingItem Then

Debug.Print myMail.Subject

Else

Stop

End If

Next myItem

End Sub

© 著作权归作者所有,转载或内容合作请联系作者

推荐阅读 更多精彩内容