2014-01-30 78 views
0

我正在構建一個代碼,用於在Outlook文件夾中執行搜索,並將這些項目的主體放在一起(僅爲一個項目構建歷史記錄)。Outlook VBA按日期排序

我正在使用Find方法執行搜索(不確定是否全部正確)。一旦我得到搜索結果,我會把它們放入一個數組中。

有沒有辦法按日期排序數組?調用find前

Dim olApp As Outlook.Application 
Dim olNs As Outlook.Namespace 
Dim olFldr As Outlook.MAPIFolder 
Dim olItms As Outlook.Items 
Dim olMail As Variant 
Dim MyArray() As String 


Set olApp = New Outlook.Application 
Set olNs = olApp.GetNamespace(」MAPI」) 
Set olFldr = olNs.GetDefaultFolder(olFolderInbox) 
Set olItms = olFldr.Items 


Set olMail = myTasks.Find("[Subject] = ""*140115LS*""") 
    If Not (olMail Is Nothing) Then 

MyArray = olMail.Display 
+0

你的意思是'olItms.Find(...)'而不是'myTasks.Find(...)'? –

+0

哦,是的,我幾分鐘前改變了變量! TKS! 關於如何按日期排序的想法? – AndroidDev

回答

1
'... 
Dim sFilter as string 
sFilter = "@SQL=""urn:schemas:mailheader:subject"" like '%140115LS%' " 

Set olFldr = olNs.GetDefaultFolder(olFolderInbox) 

Set olItms = olFldr.Items 
Set olMail = olItms.Find(sFilter) 

Do While Not olMail Is Nothing 
    'add to array... 

    Set olMail = olItms.FindNext 
Loop 
'... 
+0

這不起作用 - 每次調用olFldr.Items時,都會返回一個全新的COM對象,但不知道以前的實例。您最終會對Items對象的差異實例調用FindNext。您需要將Items集合緩存到單獨的變量中,就像原始的海報一樣。 –

+0

@DmitryStreblechenko - 謝謝 - 將編輯我的回覆 –

0

呼叫olItms.Sort("[ReceivedTime]"):我不斷收到一個錯誤與下面的代碼。您還需要在循環中調用FindNext,直到它返回null。

+0

我認爲你的意思是:olItms.Sort(「[ReceivedTime]」)... ReceivedDate不是Outlook 2013中的一個屬性。(但也許是以前的版本) – lukehawk

+0

是的,你是對的,它是ReceivedTime。我更新了他的回答。 –