在方案儲存體中儲存資料夾的自動封存屬性
本主題所顯示的方案會將其私人資料儲存在一些 MAPI 自動封存屬性中。 解決方案會將這些屬性儲存在套用自動封存屬性之資料夾的 StorageItem 物件中。 StorageItem 物件會當做隱藏資料儲存在資料夾的相關部分裡,而且因為方案可以選擇性地加密資料,所以能夠提供方案資料所需的隱私。 因為 MAPI 自動封存屬性不會在 Outlook 物件模型中公開為明確的內建屬性,所以解決方案會使用StorageItem物件上的PropertyAccessor來設定這些屬性。
程序的說明如下:
- 函
ChangeAgingProperties
式接受下列作為輸入參數:
oFolder
是套用過時屬性的 Folder 物件,也是儲存這些屬性值的位置。AgeFolder
會指出是要依照指定封存資料夾中的項目,還是要刪除這些項目。DeleteItems
會指出是否要刪除那些超過過時期限的項目,而不是封存這些項目。FileName' 表示封存過時專案的特定檔案。 如果這是一個空字串,則會使用預設的封存檔案 archive.pst。
Granularity
會指出過時的時間單位,決定是要以月、週或日為單位來計算封存。Period
會指出指定資料細微性中的時間量。Granularity
Period
和 值一起表示過時期間。 指定資料夾中早于此過時期間的專案會依指定進行封存或刪除。 例如,Granularity
2 的 和Period
14 的 指定過時期間為 14 天,當指定資料夾中超過 14 天的專案應依指定進行封存或刪除時。Default
會指出應該要將哪些設定設為預設值。 可能的值是 0、1 和 3:0 表示沒有設定預設值。
1 表示只有檔案位置有預設值。 這個功效等同於您在資料夾的 [內容] 對話方塊的 [自動封存] 索引標籤中,選取 [使用這些設定封存此資料夾] 和 [將舊項目移到預設封存資料夾]。
3 表示所有設定都有預設值。 這個功效等同於您在資料夾的 [內容] 對話方塊中,選取 [自動封存] 索引標籤上的 [使用預設設定封存此資料夾]。
檢查參數的有效性。
如果參數有效,則會使用 Folder.GetStorage 來建立或取得具有郵件類別 IPC.MS.Outlook.AgingProperties 的現有 StorageItem 物件。
接著,會使用 PropertyAccessor 來設定 StorageItem 上的自動封存屬性,並使用 StorageItem.Save 來儲存對 StorageItem 所做的變更。
此
TestAgingProps
程式會設定目前資料夾過時屬性的自動封存設定,以便將六個月以上的專案移至預設封存檔案。
註解
將程式碼放置在內建的 ThisOutlookSession 模組中。
執行程式,
TestAgingProps
在使用中檔案總管的目前資料夾上設定過時屬性。
注意 無論是實作為 VBA 宏或 COM 增益集,解決方案都是受信任的呼叫者,因此可以存取 PropertyAccessor。 若要改善此範例,請將下列 VBA 程式碼包裝在 .NET 類別中,以獲得較佳的錯誤捕捉及 Granularity 的列舉。
Function ChangeAgingProperties(oFolder As Outlook.Folder, _
AgeFolder As Boolean, DeleteItems As Boolean, _
FileName As String, Granularity As Integer, _
Period As Integer, Default As Integer) As Boolean
'6 MAPI properties for aging items in a folder
Const PR_AGING_AGE_FOLDER = _
"https://schemas.microsoft.com/mapi/proptag/0x6857000B"
Const PR_AGING_DELETE_ITEMS = _
"https://schemas.microsoft.com/mapi/proptag/0x6855000B"
Const PR_AGING_FILE_NAME_AFTER9 = _
"https://schemas.microsoft.com/mapi/proptag/0x6859001E"
Const PR_AGING_GRANULARITY = _
"https://schemas.microsoft.com/mapi/proptag/0x36EE0003"
Const PR_AGING_PERIOD = _
"https://schemas.microsoft.com/mapi/proptag/0x36EC0003"
Const PR_AGING_DEFAULT = _
"https://schemas.microsoft.com/mapi/proptag/0x685E0003"
Dim oStorage As StorageItem
Dim oPA As PropertyAccessor
' Valid Period:
' 1-999
'
' Valid Granularity:
' 0=Months, 1=Weeks, 2=Days
'
' Valid Default:
' 0=All settings don't use a default setting
' 1=Only the file location is defaulted
' "Archive this folder using these settings" and
' "Move old items to default archive folder" are checked
' 3=All settings are defaulted
' "Archive items in this folder using default settings" is checked
If (oFolder Is Nothing) Or _
(Granularity < 0 Or Granularity > 2) Or _
(Period < 1 Or Period > 999) Or _
(Default < 0 Or Default = 2 Or Default > 3) _
Then
ChangeAgingProperties = False
End If
On Error GoTo Aging_ErrTrap
'Create or get solution storage in given folder by message class
Set oStorage = oFolder.GetStorage( _
"IPC.MS.Outlook.AgingProperties", olIdentifyByMessageClass)
Set oPA = oStorage.PropertyAccessor
If Not (AgeFolder) Then
oPA.SetProperty PR_AGING_AGE_FOLDER, False
Else
'Set the 6 aging properties in the solution storage
oPA.SetProperty PR_AGING_AGE_FOLDER, True
oPA.SetProperty PR_AGING_GRANULARITY, Granularity
oPA.SetProperty PR_AGING_DELETE_ITEMS, DeleteItems
oPA.SetProperty PR_AGING_PERIOD, Period
If FileName <> "" Then
oPA.SetProperty PR_AGING_FILE_NAME_AFTER9, FileName
End If
oPA.SetProperty (PR_AGING_DEFAULT), Default
End If
'Save changes as hidden messages to the associated portion of the folder
oStorage.Save
ChangeAgingProperties = True
Exit Function
Aging_ErrTrap:
Debug.Print Err.Number, Err.Description
ChangeAgingProperties = False
End Function
Sub TestAgingProps()
Dim oFolder As Outlook.Folder
Set oFolder = Application.ActiveExplorer.CurrentFolder
If ChangeAgingProperties(oFolder, True, False, "", 0, 6, 1) Then
Debug.Print "ChangeAgingProperties OK"
Else
Debug.Print "ChangeAgingProperties Failed"
End If
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應