【问题标题】:Updating existing custom properties word doc更新现有的自定义属性 word doc
【发布时间】: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 文件

标签: c# ms-word


【解决方案1】:

不知道您是否找到了正确答案,但对于访问此页面并希望设置现有自定义文档属性的任何人。看来您需要先使用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 });
    }

【讨论】:

    【解决方案2】:

    我们通常将所有道具检索到列表中并从文档中删除,更改值并再次插入。我们使用 DSOFile.dll 方法

    【讨论】:

    • 如何删除所有自定义属性?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多