C# 更新现有自定义属性word文档
我正试图通过编程在word.doc(word 97-2003)上更新现有的自定义属性。我最初用Aspose解决了这个问题,但由于许可证有限,我不能将它用于这个项目 这段代码是从这里大量获取的,对word而不是excel做了一些小的修改 第一个方法用于添加自定义属性。如果自定义属性不存在,第二个方法可以提取自定义属性。我还没有弄明白如何更新已经存在的属性。我认为这可能与InvokeMember()中的动词有关,但我找不到太多文档C# 更新现有自定义属性word文档,c#,ms-word,C#,Ms Word,我正试图通过编程在word.doc(word 97-2003)上更新现有的自定义属性。我最初用Aspose解决了这个问题,但由于许可证有限,我不能将它用于这个项目 这段代码是从这里大量获取的,对word而不是excel做了一些小的修改 第一个方法用于添加自定义属性。如果自定义属性不存在,第二个方法可以提取自定义属性。我还没有弄明白如何更新已经存在的属性。我认为这可能与InvokeMember()中的动词有关,但我找不到太多文档 public void SetDocumentProperty
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;
}
我们通常检索列表中的所有道具并从文档中删除,更改值并再次插入。我们使用DSOFile.dll方法不知道您是否找到了正确答案,但对于访问此页面希望设置现有自定义文档属性的任何人来说。似乎需要首先使用
BindingFlags.GetProperty
检索文档属性,然后使用BindingFlags.SetProperty
设置检索到的特定项的值
您还可以添加一些自定义检查,以确定您尝试设置的对象是否有效
public void SetDocumentProperty(string propertyName, object value)
{
// Get all the custom properties
object customProperties = wordDocument.CustomDocumentProperties;
Type customPropertiesType = customProperties.GetType();
// Retrieve the specific custom property item
object customPropertyItem = customPropertiesType.InvokeMember("Item",
BindingFlags.Default | BindingFlags.GetProperty, null, customProperties,
new object[] { propertyName });
Type propertyNameType = customPropertyItem.GetType();
// Set the value of the specific custom property item
propertyNameType.InvokeMember("Value", BindingFlags.Default | BindingFlags.SetProperty, null,
customPropertyItem, new object[] { value });
}
这个库看起来不错,但我需要一个.doc文件。如何删除所有自定义属性?