If the CustomProperty changed, does the docx file changes too?

Jun 8, 2010 at 4:33 PM

If I open a docx file using DocX and then change the CustomProperty by using AddCustomProperty method.  If there are some DocProperty field defined in the document, does all the text be changed after docx file saved?  If not, how to apply these changes to reflect to the document?

Thanks.

Coordinator
Jun 8, 2010 at 11:49 PM
Hi doggy8088,

the following piece of code does what you want.

// Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
    // Update or Add the custom property Name.
    document.AddCustomProperty(new CustomProperty("Name", "doggy8088"));
                
    // Save all changes made to this document.
    document.SaveAs("Test2.docx");
}

This is what the input file looks like (Test.docx)
Before.png

Here is the output of running the above code (Test2.docx)

After.png

As you can see the field values are auto updated.

kind regards,
Cathal
Jun 9, 2010 at 1:29 AM

Hi coffeycathal,

Although I can't see your screenshot, but I understand what you said.  I'll take a try on it.  Thanks! 

By the way, I know your AddCustomProperty will remove the current CustomProperty and insert a new one into document.  I would like to recommand you to use SetCustomProperty or InsertCustomProperty as the method name to avoid ambiguous meaning.
For example, in the .NET 1.1, the Cache.Add will cause exception when key has been already exists and Cache.Insert will just replace it.

It will be better if you can support document.CustomProperty["Name"] = "doggy8088"; to insert or replace a CustomProperty.

I think DocX is a very good project.  You did a good job.  Thanks!

best regards,
Will

Mar 9, 2011 at 7:43 AM
Edited Mar 9, 2011 at 9:12 AM

If the custom property is inside a table, the change does not reflect (the change has to be manually updated) to the document. Is it a bug?