能否通过代码修改已添加的Outlook MailItem自定义属性类型?
Outlook自定义属性类型能否修改?
不能直接修改已保存的自定义属性类型。
当你通过以下代码创建并保存了olNumber类型的"rating"属性后:
mailItem.UserProperties.Add("rating", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olNumber, false, 1); mailItem.Save();
该属性的数据类型会被固定,Outlook不支持直接更改现有自定义属性的类型。
替代方案
要切换为olText类型,需先删除原有属性,再重新添加同名的olText类型属性。如果需要保留原有数据,要先读取原属性值并转换类型后再赋值给新属性:
保留数据的实现代码:
// 读取原属性值(如果存在) string oldRatingValue = string.Empty; Outlook.UserProperty existingRatingProp = mailItem.UserProperties["rating"]; if (existingRatingProp != null) { oldRatingValue = existingRatingProp.Value.ToString(); existingRatingProp.Delete(); // 删除原属性 } // 添加olText类型的新属性并赋值 Outlook.UserProperty newRatingProp = mailItem.UserProperties.Add( "rating", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, 1 ); newRatingProp.Value = oldRatingValue; mailItem.Save();
不保留数据的简化代码:
Outlook.UserProperty existingProp = mailItem.UserProperties["rating"]; if (existingProp != null) { existingProp.Delete(); } mailItem.UserProperties.Add("rating", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, 1); mailItem.Save();
内容的提问来源于stack exchange,提问作者user964829
相关产品推荐
相关产品推荐

