2017-05-16 1 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

당신은 오히려 컬렉션의 항목보다 폴더의 "원시"항목을 처리에 복귀. True/False는 폴더의 항목에 영향을주지 않습니다.

이메일 항목을 통해 루프 상승하고 빠른 방법에 대한
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

Niton에게 감사드립니다. 완벽하게 작동합니다. – Gandalf

1

-

필터 (strFind)

그것을 속도를 함께 SenderName 제한 또한, 역 루프를 사용해보십시오

예는

,451,515,
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 

올바른 이름

관련 문제