You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过代码修改已添加的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 20:27:49