1

我正在尝试以编程方式更新 word .doc(word 97 - 2003)上的现有自定义属性。我最初用 Aspose 解决了,但由于许可证有限,我不能将它用于这个项目。

此代码是从这里批发的,对 word 进行了少量修改,而不是 excel 以编程方式访问 Excel 自定义文档属性

如果自定义属性不存在,第一种方法可以添加自定义属性,第二种方法可以拉取自定义属性。我还没有弄清楚如何更新已经存在的属性。我认为这可能与 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;
    }
4

2 回答 2

3

不知道您是否找到了正确答案,但对于访问此页面并希望设置现有自定义文档属性的任何人。似乎您需要先使用 检索文档属性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 });
    }
于 2014-10-30T15:03:55.620 回答
0

We normally retrieve all the props to a list and delete from document, change values and insert again. We use DSOFile.dll approach

于 2013-05-08T14:47:38.673 回答