I am trying to update existing custom properties programatically on a word .doc (word 97 - 2003). I initially solved with Aspose but due to limited licenses I can't use it for this project.
This code is taken wholesale from here with minor modifications for word instead of excel Accessing Excel Custom Document Properties programatically.
The first method works to add a custom property if it does not exist, the second can pull custom properties. I have not figured out how to update a property that already exists. I think it might have to do with the verb in InvokeMember() but I can't find much documentation.
public void SetDocumentProperty(string propertyName, string propertyValue)
{
object oDocCustomProps = oWordDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
object[] oArgs = {propertyName,false,
MsoDocProperties.msoPropertyTypeString,
propertyValue};
typeDocCustomProps.InvokeMember("Add",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
oDocCustomProps,
oArgs);
}
public object GetDocumentProperty(string propertyName, MsoDocProperties type)
{
object returnVal = null;
object oDocCustomProps = oWordDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
object returned = typeDocCustomProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty, null,
oDocCustomProps, new object[] { propertyName });
Type typeDocAuthorProp = returned.GetType();
returnVal = typeDocAuthorProp.InvokeMember("Value",
BindingFlags.Default |
BindingFlags.GetProperty,
null, returned,
new object[] { }).ToString();
return returnVal;
}