Reputation: 137
Is there a means of testing whether a property exists, prior to attempting to get the property through the PropertyAccessor?
I utilise the function below, using the Outlook PropertyAccessor, to return a string value of a property. If the property doesn't exist, the function catches the error and returns a null value string. I write out the error using the debug.writeline method to visibly identify what error is thrown - the only error that I really come across is Exception thrown: 'System.Runtime.InteropServices.COMException and that usually is related to the property being unknown or cannot be found. The DASL being passed and property names are correct and work - however not all emails have these properties - they are created by an independent software vendor.
...
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;
}
When a property does not exist, an exception is thrown, and whilst it is caught, it doesn't seem to be (properly)handled - the output being:
Exception thrown: 'System.Runtime.InteropServices.COMException' in Office777.dll
OutlookProperties - GetPropertyString() - Error:=The property "http://schemas.microsoft.com/mapi/string/{41FFBD02-4241-4EBD-A7B3-93DD2DF86CA9}/CaseGUID" is unknown or cannot be found.
Many thanks in Advance
Upvotes: 1
Views: 461
Reputation: 66276
Handling the exception is the only way - older versions of Outlook used to return null, but the latest versions always throw the exception. Checking COMException.ErrorCode
won't help either: it is usually 0x80020009 (DISP_E_EXCEPTION
) rather than something more informative, like MAPI_E_NOT_FOUND
.
Upvotes: 2