【问题标题】:Outlook VSTO - testing whether a property exists, prior to attempting to get the property through the PropertyAccessorOutlook VSTO - 在尝试通过 PropertyAccessor 获取属性之前测试属性是否存在
【发布时间】:2020-05-04 02:23:08
【问题描述】:

在尝试通过 PropertyAccessor 获取属性之前,是否有测试属性是否存在的方法?

我利用下面的函数,使用 Outlook PropertyAccessor,返回属性的字符串值。如果该属性不存在,该函数将捕获错误并返回一个空值字符串。我使用 debug.writeline 方法写出错误以明显识别抛出的错误 - 我真正遇到的唯一错误是 Exception throw: 'System.Runtime.InteropServices.COMException 并且通常是与未知或无法找到的财产​​有关。传递的 DASL 和属性名称正确且有效 - 但并非所有电子邮件都具有这些属性 - 它们是由独立软件供应商创建的。

    ...
    using Outlook = Microsoft.Office.Interop.Outlook;
    ...
    private string GetPropertyString(Outlook.PropertyAccessor pa, string property)
    {
        string retVal = null;

        try
        {
            retVal = (string)pa.GetProperty(property);
        }
        catch (Exception ex)
        {
            retVal = null;
            Debug.WriteLine("OutlookProperties - GetPropertyString() - Error:=" + ex.Message);
            //throw;
        }
        finally
        {

        }
        return retVal;
    }

当一个属性不存在时,抛出一个异常,当它被捕获时,它似乎没有(正确)处理 - 输出是:

抛出异常:Office777.dll 中的“System.Runtime.InteropServices.COMException”

OutlookProperties - GetPropertyString() - 错误:=属性 "http://schemas.microsoft.com/mapi/string/{41FFBD02-4241-4EBD-A7B3-93DD2DF86CA9}/CaseGUID" 未知或找不到。

非常感谢提前

【问题讨论】:

  • 异常处理不当是什么意思?

标签: c# outlook vsto


【解决方案1】:

处理异常是唯一的方法 - 旧版本的 Outlook 过去常常返回 null,但最新版本总是抛出异常。检查 COMException.ErrorCode 也无济于事:它通常是 0x80020009 (DISP_E_EXCEPTION),而不是像 MAPI_E_NOT_FOUND 这样提供更多信息的东西。


【讨论】:

  • 谢谢@Dimitry,您会以与上面的代码不同的方式处理异常吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 2020-03-21
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多