PropertyAccessor.SetProperties 方法 (Outlook)

将由数组 SchemaNames 指定的属性设置为由数组 Values 指定的值。

语法

expressionSetProperties( _SchemaNames_ , _Values_ )

表达 一个代表 PropertyAccessor 对象的变量。

参数

名称 必需/可选 数据类型 说明
SchemaNames 必需 Variant 属性名称的数组,其值将设置为由 Values 参数指定。 这些属性通过命名空间引用。 有关详细信息,请参阅通过命名空间引用属性
必需 Variant 要为 SchemaNames 参数指定的属性设置的值数组。

返回值

如果操作成功,则为 Null (VBA 中没有内容) 变量。 如果之前设置任何属性时出现错误,例如, SchemaNames数组中元素的个数不匹配,在 Values数组中,并将返回 Err 值。 如果在属性的设置时出现错误,返回值是 Err 对象,正在与 SchemaNames数组的相同此数组中的元素数的数组。 数组中的 错误 值被映射到在 SchemaNames参数中设置相应的属性的错误结果。

备注

如果该属性不存在,并且 SchemaNames 元素包含有效的属性说明符,则 SetProperties 将创建属性,并使用 Values 指定的值分配该属性。 该属性的类型将与在 Values 中传递的元素的类型相同。 如果该属性的确存在,则 SetProperties 为该属性分配由 Values 指定的值。

请注意,使用 PropertyAccessor 创建的自定义属性在自定义视图中不受支持。 如果想查看某个项目的自定义属性,可使用 UserProperties 对象的 Add 方法创建该属性。

如果父对象的 PropertyAccessor 支持显式 保存 操作,则属性应保存对该对象具有显式 保存 方法调用。 如果该对象不支持显式 Save 操作,则属性保存到该对象在调用 SetProperties 时。

处理异常时要多加小心,并确保所有异常都已正确处理。 设置属性失败的情况包括:

  • 属性是只读的,因为一些 Outlook 和 MAPI 属性是只读的。

  • 找不到通过指定命名空间引用的属性。

  • 属性是以无效格式指定的,无法进行分析。

  • 属性不存在且无法创建。

  • 属性存在,但传递给它的值类型不正确。

  • 由于客户端脱机,无法打开属性。

  • 属性是使用 UserProperties.Add 方法创建的。 首次设置属性时,必须使用 UserProperty.Value 属性,而不是 PropertyAccessor 对象的 SetPropertiesSetProperty 方法。

有关使用 PropertyAccessor 对象设置属性的详细信息,请参阅 获取和设置属性的最佳做法

示例

此代码示例演示的 SetProperties 方法将多个属性值的设置。 如果属性不存在, SetProperties 将创建该属性,只要父对象支持属性创建。 由于 MailItem 对象支持 MailItem.Save 操作,下面的属性显式 oMail.Save一起保存。

Sub DemoPropertyAccessorSetProperties() 
 Dim PropNames(), myValues() As Variant 
 Dim arrErrors As Variant 
 Dim prop1, prop2, prop3, prop4 As String 
 Dim i As Integer 
 Dim oMail As Outlook.MailItem 
 Dim oPA As Outlook.PropertyAccessor 
 'Get first item in the inbox 
 Set oMail = _ 
 Application.Session.GetDefaultFolder(olFolderInbox).Items(1) 
 'Names for properties using the MAPI string namespace 
 prop1 = "http://schemas.microsoft.com/mapi/string/" & _ 
 "{FFF40745-D92F-4C11-9E14-92701F001EB3}/mylongprop" 
 prop2 = "http://schemas.microsoft.com/mapi/string/" & _ 
 "{FFF40745-D92F-4C11-9E14-92701F001EB3}/mystringprop" 
 prop3 = "http://schemas.microsoft.com/mapi/string/" & _ 
 "{FFF40745-D92F-4C11-9E14-92701F001EB3}/mydateprop" 
 prop4 = "http://schemas.microsoft.com/mapi/string/" & _ 
 "{FFF40745-D92F-4C11-9E14-92701F001EB3}/myboolprop" 
 PropNames = Array(prop1, prop2, prop3, prop4) 
 myValues = Array(1020, "111-222-Kudo", Now(), False) 
 'Set values with SetProperties call 
 'If the properties don't exist, then SetProperties 
 'adds the properties to the object when saved. 
 'The type of the property is the type of the element 
 'passed in myValues array. 
 Set oPA = oMail.PropertyAccessor 
 arrErrors = oPA.SetProperties(PropNames, myValues) 
 If Not (IsEmpty(arrErrors)) Then 
 'Examine the arrErrors array to determine if any 
 'elements contain errors 
 For i = LBound(arrErrors) To UBound(arrErrors) 
 'Examine the type of the element 
 If IsError(arrErrors(i)) Then 
 Debug.Print (CVErr(arrErrors(i))) 
 End If 
 Next 
 End If 
 'Save the item 
 oMail.Save 
End Sub

另请参阅

PropertyAccessor 对象

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。