共用方式為


列舉連絡人資料夾,並只設定連絡人項目的自訂屬性

Outlook 資料夾可以包含多個郵件類別的專案。 例如,根據預設,您可以建立 [連絡人] 資料夾中的連絡人項目及通訊群組清單項目。 如果要有系統地只將動作套用至連絡人項目或資料夾中的通訊群組清單項目,您必須檢查資料夾中每個項目的郵件類別,然後再套用動作。

在這個主題的程式碼範例中,會使用項目的郵件類別識別連絡人項目,並為 [連絡人] 資料夾中的所有連絡人項目設定使用者定義的 Affiliation 欄位。 程序說明如下:

  1. 程式碼範例會取得預設 [連絡人] 資料夾中的所有項目。

  2. 使用 Items.Restrict ,從預設 [連絡人] 資料夾的所有項目中篩選出連絡人項目。

  3. 對於每個連絡人項目,會使用 UserProperties.Add 新增使用者定義的欄位 Affiliation ,並根據是否有住家電話號碼設定之。 如果項目沒有家用電話號碼, Affiliation 屬性會設為 Business ,否則會設為 Personal

註解

若要執行這個程式碼範例,請將程式碼置於內建的 ThisOutlookSession 模組中。 執行 SetAffiliationForContacts 程序。

請注意,如果名為 Affiliation 的欄位已存在,執行這個範例會覆寫它。

Sub SetAffiliationForContacts() 
 Dim ns As NameSpace 
 Dim foldContact As Folder 
 Dim itemContact As ContactItem 
 Dim colItems As Outlook.Items 
 Dim myProperty As Outlook.UserProperty 
 
 Set ns = Application.GetNamespace("MAPI") 
 Set foldContact = ns.GetDefaultFolder(olFolderContacts) 
 Set colItems = foldContact.Items.Restrict("[MessageClass]='IPM.Contact'") 
 
 For Each itemContact In colItems 
 ' Add user property to contact items 
 Set myProperty = itemContact.UserProperties.Add("Affiliation", olText) 
 If itemContact.HomeTelephoneNumber = "" Then 
 myProperty = "Business" 
 Else 
 myProperty = "Personal" 
 End If 
 itemContact.Save 
 Next 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應