views:

347

answers:

1

Hi all, I'm trying to modify some CustomDocumentProperties for a .docx document. So far I've been able to read the current value and modify it, but, for whatever reason, whenever I save the document the changes to the custom fields are lost.

I have the following method within a 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

My doc_accessor.SaveAndClose() function is working correctly as I modified the path to save to a different location and it did... but without writing the modified CustomDocumentProperties. This makes it seem as if there's a commit step of sorts that I'm missing, but shouldn't current_doc.Save() handle that?

Any help, advice or corrections are welcome!

+1  A: 

http://support.microsoft.com/kb/195425

http://msdn.microsoft.com/en-us/library/y1xatbkd%28VS.80%29.aspx

i dunno if either of these will help. but that's where i would start.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documentclass.saved%28office.11%29.aspx

Sorry about the links i've had to remove the protocol heading because stack doesn't think i should be able to have more than one link in my answers because i'm not a real member

Dave
Thank you! setting current_doc.Saved to false did the trick.
MrSpo