Page 1 of 1

word document template - properties update - requires manual

PostPosted: Fri Nov 11, 2011 2:40 am
by drgiri
I am using custom properties in a word document and fill the properties value through a custom java application. The code works and the output written without errors.
But when I open the word document I do not see the updated values until I right click on those property fields and update them. Not able to figure out why.
I have given my code below. Am I need to do any update or refresh the values in the code ?

Thanks and Regards
DRG

WordprocessingMLPackage wordMLPackage=null;
org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart=null;
try
{
// Load the docx package
wordMLPackage = WordprocessingMLPackage.load(new java.io.File(strFullFileName));

docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();
docPropsCustomPart.setProperty("EmpFName", "RichieRich");
docPropsCustomPart.setProperty("BankAccountLongName", "GreeceNationalBank:00123456789012");

System.out.println("AfLongName Value is:"+docPropsCustomPart.getProperty("EmpFName")); // This line prints the value correctly.
wordMLPackage.save(new java.io.File("D://TemplSubst.docx"));
}
catch(Exception ex)
{
System.out.println("Big Error: "+ex.toString());
}

Re: word document template - properties update - requires ma

PostPosted: Fri Nov 11, 2011 9:42 am
by jason
Word doesn't update fields automatically when you open a document.

You have 3 options:
1. Update fields manually (Ctrl F9 iirc)
2. Run a macro to do it (AutoOpen) - though the user will be asked whether they're happy for it to run
3. Write code using docx4j to do the updates yourself. To do this, you need to traverse the document locating the fields, then interpret them and update accordingly

cheers .. Jason

Re: word document template - properties update - requires ma

PostPosted: Fri Nov 11, 2011 6:59 pm
by drgiri
Thanks Mr.Jason. I am not very familiar with docx4j. Can you expand the statement, traverse the document location .. and interpret them ?
Do you mean that I replace the field values with the required value string in that place ?

TIA
DRG

Re: word document template - properties update - requires ma

PostPosted: Sat Sep 10, 2016 5:38 am
by Srivaar
Hi Giri,

I'm trying to do the same and wondering if you could able to do it. Thanks,

Sri

Re: word document template - properties update - requires ma

PostPosted: Sun Sep 11, 2016 4:04 am
by jason
You have resurrected a really old thread here, so I'm locking it. For anything more on this topic, feel free to open a new one.

These days, you should be able to use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                // Refresh the values of DOCPROPERTY fields
                FieldUpdater updater = new FieldUpdater(wordMLPackage);
                updater.update(true);
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4