2017-05-16 37 views
1

我需要在Outlook中以升序模式循環使用vba腳本。循環和整理電子郵件項目

我嘗試了幾種方法,但似乎它總是以降模式循環。

有更快的方式來循環瀏覽電子郵件項目嗎?

謝謝。 的代碼是一樣的東西:

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 
+0

此示例代碼不運行更新%Client1%。見[mcve]。 – niton

回答

1

您收歸處理文件夾中的「原始」的項目,而不是集合中的項目。真/假對文件夾中的項目沒有影響。

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 
+0

謝謝尼通,它完美的作品。 – Gandalf

1

的上升和更快的方式依次通過電子郵件項目 -

嘗試使用反向循環的,也限制SenderName您的篩選器(strFind)加快步伐

示例將是

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 

確保以正確的名稱

相關問題