【问题标题】:Custom properties are not updated for the word via openXML不会通过 openXML 更新单词的自定义属性
【发布时间】:2016-05-02 06:30:02
【问题描述】:

我正在尝试通过 Open XML 编程更新 word 文档的自定义属性,但似乎更新后的属性没有为 word 文档正确保存。因此,当我在成功执行更新自定义属性代码后打开文档时,我收到消息框“此文档包含可能引用其他文件的字段;您要更新文档中的字段吗?”如果我按下“否”按钮,则所有更新属性都不会保存到文档中。如果我们选择 yes 选项,那么它将更新属性,但我需要明确保存属性。请建议将属性保存到文档中,而不会收到确认消息或损坏文档。 :)

代码sn-p如下,

public void SetCustomValue(
    WordprocessingDocument document, string propname, string aValue)
{
    CustomFilePropertiesPart oDocCustomProps = document.CustomFilePropertiesPart;

    Properties props = oDocCustomProps.Properties;

    if (props != null)
    {
        //logger.Debug("props is not null");
        foreach (var prop in props.Elements<CustomDocumentProperty>())
        {
            if (prop != null && prop.Name == propname)
            {
                //logger.Debug("Setting Property: " + prop.Name + " to value: " + aValue);
                prop.Remove();
                var newProp = new CustomDocumentProperty();
                newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";
                newProp.Name = prop.Name;
                VTLPWSTR vTLPWSTR1 = new VTLPWSTR();
                vTLPWSTR1.Text = aValue;
                newProp.Append(vTLPWSTR1);
                props.AppendChild(newProp);
                props.Save();
            }
        }

        int pid = 2;

        foreach (CustomDocumentProperty item in props)
        {                        
            item.PropertyId = pid++;
        }

        props.Save();
    }
}

我正在使用带有 Open XML SDK 2.0 和 Office 2013 的 .Net framework 3.5。

【问题讨论】:

    标签: c#-3.0 office-interop openxml-sdk


    【解决方案1】:

    试试这个

            var CustomeProperties = xmlDOc.CustomFilePropertiesPart.Properties;
    
            foreach (CustomDocumentProperty customeProperty in CustomeProperties)
            {
                if (customeProperty.Name == "DocumentName")
                {
                    customeProperty.VTLPWSTR = new VTLPWSTR("My Custom Name");
                }
                else if (customeProperty.Name == "DocumentID")
                {
                    customeProperty.VTLPWSTR = new VTLPWSTR("FNP.SMS.IQC");
                }
                else if (customeProperty.Name == "DocumentLastUpdate")
                {
                    customeProperty.VTLPWSTR = new VTLPWSTR(DateTime.Now.ToShortDateString());
                }
            }
    
            //Open Word Setting File
            DocumentSettingsPart settingsPart = xmlDOc.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
            //Update Fields
            UpdateFieldsOnOpen updateFields = new UpdateFieldsOnOpen();
            updateFields.Val = new OnOffValue(true);
    
            settingsPart.Settings.PrependChild<UpdateFieldsOnOpen>(updateFields);
            settingsPart.Settings.Save();
    

    您必须在打开时更新您的文档字段。

    【讨论】:

    • 这似乎导致文档为只读,并在用户打开文档时询问是否更新字段。这也是问题所要问的。
    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多