Looping and sorting email items

只愿长相守 提交于 2020-06-28 05:41:56

问题


I need to loop in a vba script in Outlook in ascending mode.

I tried several ways but it seems it always loop in descending mode.

Is there a faster way to loop through the email items?

Thanks. The code is something like:

Public Sub CheckClient()
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim items As Outlook.items
Dim strFind As String
Dim Item

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.PickFolder()

strFind = "[ReceivedTime] >= '05/15/2017' AND [ReceivedTime] < '05/16/2017'"

Set items = objFolder.items
items.Sort "[ReceivedTime]", True

Set items = objFolder.items.Restrict(strFind)

For Each Item In objFolder.items
    If TypeName(Item) = "MailItem" Then

            If Item.Sender = "Client1"  Then
                DBInsert (Item)
            End if
Next
End Sub

回答1:


You reverted to processing the "raw" items in the folder rather than the items in the collection. True/False has no impact on the items in the folder.

Sub CheckClient()

Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim items As Outlook.items

Dim strFind As String
Dim Item As Object
Dim resItems As items

Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.PickFolder()

strFind = "[ReceivedTime] >= '05/15/2017' AND [ReceivedTime] < '05/16/2017'"

Set items = objFolder.items

items.Sort "[ReceivedTime]", True
For Each Item In items
    If TypeName(Item) = "MailItem" Then
        Debug.Print Item.ReceivedTime & ": " & Item.Subject
    End If
Next

Debug.Print

Set resItems = objFolder.items.Restrict(strFind)
' False should sort in reverse order of True
resItems.Sort "[ReceivedTime]", False

' Process resItems not the entire folder
For Each Item In resItems
    If TypeName(Item) = "MailItem" Then
        Debug.Print Item.ReceivedTime & ": " & Item.Subject
    End If
Next
End Sub



回答2:


for ascending and faster way to loop through the email items-

Try using reverse loop, also Restrict SenderName with your filter ( strFind ) to speed it up

Example would be

Option Explicit
Public Sub CheckClient()
    Dim objFolder As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim strFind As String
    Dim Recived As Long
    Dim i As Long

    Set objFolder = Application.Session.PickFolder
    Set Items = objFolder.Items
        Items.Sort "[ReceivedTime]"

    strFind = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
                        Chr(34) & " >= '05/15/2017' AND " & _
                        Chr(34) & "urn:schemas:httpmail:datereceived" & _
                        Chr(34) & " < '05/16/2017' AND " & _
                        Chr(34) & "urn:schemas:httpmail:fromname" & _
                        Chr(34) & "Like '%Client1%'"

    Set Items = objFolder.Items.Restrict(strFind)

    For i = Items.Count To 1 Step -1
        DoEvents
        Debug.Print Items(i).SenderName 'Immediate Window
        Debug.Print Items(i).ReceivedTime 'Immediate Window
    Next

    Set objFolder = Nothing
    Set Items = Nothing
End Sub

Make sure to update %Client1% with correct name



来源:https://stackoverflow.com/questions/44003384/looping-and-sorting-email-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!