使用 Table 物件以高效能列舉資料夾中的篩選項目
在這個主題的程式碼範例中,會使用 Table 物件列舉 [收件匣] 中上次修改時間為 2005 年 5 月 1 日之後的一組經過篩選的項目。 在程式碼範例中,會列印每個項目的下列值:主旨、上次修改項目的時間,以及項目是否隱藏。 程序如下:
範例會根據郵件項目的 LastModificationTime 屬性值定義篩選。
將篩選套用至 Folder.GetTable ,並取得 [收件匣] 中滿足篩選之郵件項目子集合的 Table 。
注意 傳回的資料表包含每個篩選項目的預設屬性集: EntryID、 Subject、 CreationTime、 LastModificationTime和 MessageClass。 3.然後使用 Columns.RemoveAll 和 Columns.Add ,以實際所需的屬性更新 Table : Subject、 LastModificationTime,以及 PidTagAttributeHidden) (隱藏屬性。 如果屬性 (例如Subject、LastModificationTime) ,它會指定具有其明確內建名稱的屬性,而且只有在沒有時,才會依其命名空間 (參考屬性,例如,訊息項目的隱藏屬性) 。
注意從步驟 2的 Folder.GetTable和步驟 3 中的Columns.Add傳回的Table物件包含不同的屬性值,但適用于 [收件匣] 中同一組篩選過的專案。 4.最後,它會使用 Table.GetNextRow 列舉篩選過的專案 (直到 Table.EndOfTable 變成 true) 為止,顯示每個專案三個所需屬性的值。
Sub DemoTable()
'Declarations
Dim Filter As String
Dim oRow As Outlook.Row
Dim oTable As Outlook.Table
Dim oFolder As Outlook.Folder
'Get a Folder object for the Inbox
Set oFolder = Application.Session.GetDefaultFolder(olFolderInbox)
'Define Filter to obtain items last modified after May 1, 2005
Filter = "[LastModificationTime] > '5/1/2005'"
'Restrict with Filter
Set oTable = oFolder.GetTable(Filter)
'Remove all columns in the default column set
oTable.Columns.RemoveAll
'Specify desired properties
With oTable.Columns
.Add ("Subject")
.Add ("LastModificationTime")
'PidTagAttributeHidden referenced by the MAPI proptag namespace
.Add ("https://schemas.microsoft.com/mapi/proptag/0x10F4000B")
End With
'Enumerate the table using test for EndOfTable
Do Until (oTable.EndOfTable)
Set oRow = oTable.GetNextRow()
Debug.Print (oRow("Subject"))
Debug.Print (oRow("LastModificationTime"))
Debug.Print (oRow("https://schemas.microsoft.com/mapi/proptag/0x10F4000B"))
Loop
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應