Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新现有自定义属性word文档_C#_Ms Word - Fatal编程技术网

C# 更新现有自定义属性word文档

C# 更新现有自定义属性word文档,c#,ms-word,C#,Ms Word,我正试图通过编程在word.doc(word 97-2003)上更新现有的自定义属性。我最初用Aspose解决了这个问题,但由于许可证有限,我不能将它用于这个项目 这段代码是从这里大量获取的,对word而不是excel做了一些小的修改 第一个方法用于添加自定义属性。如果自定义属性不存在,第二个方法可以提取自定义属性。我还没有弄明白如何更新已经存在的属性。我认为这可能与InvokeMember()中的动词有关,但我找不到太多文档 public void SetDocumentProperty

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

这段代码是从这里大量获取的,对word而不是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;
    }

我们通常检索列表中的所有道具并从文档中删除,更改值并再次插入。我们使用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文件。如何删除所有自定义属性?