【发布时间】:2014-10-30 23:09:28
【问题描述】:
我正在尝试以编程方式更新 word .doc(word 97 - 2003)上的现有自定义属性。我最初使用 Aspose 解决了问题,但由于许可证有限,我无法将其用于此项目。
此代码是从这里批发的,对 word 而不是 excel Accessing Excel Custom Document Properties programatically 进行了少量修改。
如果自定义属性不存在,第一种方法可以添加自定义属性,第二种方法可以拉取自定义属性。我还没有弄清楚如何更新已经存在的属性。我认为这可能与 InvokeMember() 中的动词有关,但我找不到太多文档。
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;
}
【问题讨论】:
-
这个库看起来不错,但我需要一个 .doc 文件