MrSpo
MrSpo

Reputation:

Saving Word 2007 Document Properties in C# 2.0

I'm trying to modify some CustomDocumentProperties for a .docx document. I've been able to read the current value and modify it, but when 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?

Upvotes: 3

Views: 1215

Answers (2)

vulcanik
vulcanik

Reputation: 98

I solved 2 minutes ago the same problem.

When you add/change custom properties seems that the document is no changed, so the WordApplication.ActiveDocument.Saved is still true.

Set it to false and then call the Save method of the Document, it will work!!

Upvotes: 1

Dave
Dave

Reputation: 11

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

http://msdn.microsoft.com/en-us/library/y1xatbkd(VS.80).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(office.11).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

Upvotes: 1

Related Questions