Saving Word 2007 Document Properties in C # 2.0

I am trying to change some CustomDocumentProperties for a .docx document. I was able to read the current value and change it, but when I save the document, changes to user fields are lost.

I have the following method in the DocAccessor class (which serves as an interface for my doc files):

void SetInfo(string key, string val) {
    object custom_properties = current_doc.CustomDocumentProperties;
    Type custom_properties_type = custom_properties.GetType();
    custom_properties_type.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, custom_properties, new object[] { key, val });
}

elsewhere i call:

doc_accessor.GetInfo("Number") //returns 5
doc_accessor.SetInfo("Number", "6");
doc_accessor.GetInfo("Number") //returns 6
doc_accessor.SaveAndClose();
doc_accessor.Open(); //it retains the path, so I don't need to respecify
doc_accessor.GetInfo("Number") //returns 5

The my doc_accessor.SaveAndClose () function works correctly, as I changed the save path to another location, and this happened ... but without writing the modified CustomDocumentProperties. This makes it seem like there is some kind of commit step that I am missing, but should current_doc.Save () not handle this?

+3

Source: https://habr.com/ru/post/1719427/


All Articles