Handle missing Word custom properties during session registration

This commit is contained in:
nick 2026-11-04 22:02:05 +00:00
parent f0d1f6be2a
commit a0c2d252e9

View File

@ -428,7 +428,19 @@ namespace PlotDirector.WordAddIn
private string ReadProperty(Word.Document document,string name)
{
object properties=null,property=null;
try{properties=Com(()=>document.CustomDocumentProperties);dynamic p=properties;property=Com<object>(()=>p[name]);dynamic value=property;return Com<string>(()=>Convert.ToString(value.Value));}
try
{
properties = Com(() => document.CustomDocumentProperties);
dynamic collection = properties;
try { property = Com<object>(() => collection[name]); }
catch (ArgumentException error) when (error.HResult == unchecked((int)0x80070057))
{
// Word can report a missing named custom property as E_INVALIDARG.
return "";
}
dynamic value = property;
return Com<string>(() => Convert.ToString(value.Value));
}
catch(COMException){return "";}finally{Release(property);Release(properties);}
}
private static void Release(object value) {if(value!=null&&Marshal.IsComObject(value))Marshal.ReleaseComObject(value);}