I'm trying to check if a custom document property has been set for an excel file or not. And if set then read the value.
Here is the code I'm using but so far no luck. It doesn't get into the foreach loop and comes out.
var propval = ReadDocumentProperty("TestProp");
private string ReadDocumentProperty(string propertyName)
{
Office.DocumentProperties properties;
Excel.Workbook Wb = Globals.ThisAddIn.Application.ActiveWorkbook;
properties = (Office.DocumentProperties)Wb.CustomDocumentProperties;
foreach (Office.DocumentProperty prop in properties)
{
if (prop.Name == propertyName)
{
return prop.Value.ToString();
}
}
return null;
}
Update1:
I found this code for setting the custom property.
Excel.Workbook workBk = Globals.ThisAddIn.Application.ActiveWorkbook;
object oDocCustomProps = workBk.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
object[] oArgs = {propertyName,false,
Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
propertyValue};
typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
BindingFlags.InvokeMethod, null,
oDocCustomProps, oArgs);
This works fine to set the custom property. I have no clue how to modify it to read the property value.