Page 1 of 1

Updating field display values in Word

PostPosted: Wed Jan 08, 2014 8:21 pm
by andyflint
Hi - me again

I am hoping that someone else has resolved an issue that I am having

Anyway, I am successfully updating properties and mergeformat fields within a word document but when I open the document the fields are still displaying the old value even though I can see in the xml that the the text has been changed.

What I would like to do is update the display when wither during or before the user opens the document and I would preferably like to do this in docx4j rather than using a macro.

I can manually update the field by right-clicking on the field and selecting update but i do not want the users to have to do this on every field.

Your reward for helping will the the usual praise and recognition from everyone who encounters the same problem with lot of thanks from me

Andy

Re: Updating field display values in Word

PostPosted: Wed Jan 08, 2014 8:41 pm
by jason
Andy, you'll need to attach a short sample docx which exhibits the issue, thanks. .. Jason

Re: Updating field display values in Word

PostPosted: Wed Jan 08, 2014 10:53 pm
by andyflint
Document attached

the field value that I want to change is EMA/655111/2013

Re: Updating field display values in Word

PostPosted: Wed Jan 08, 2014 11:12 pm
by andyflint
The attached document is post field update

Looking at the xml the text is changed to "somevalue" but the old value is still being displayed

Re: Updating field display values in Word

PostPosted: Thu Jan 09, 2014 6:01 pm
by andyflint
Probably not using the correct terminology but looking at the xml it seems that the display value has become detached from the field during the update which is why it is displaying the old value

Solved the problem by performing a text search and replace after updating the fields - fortunately the field values are very likely to be unique, not ideal but it works.

Re: Updating field display values in Word

PostPosted: Fri Jan 10, 2014 8:02 pm
by jason
In test1.docx, your field shows as an "IF" field, containing:

Code: Select all
IF EMA/655111/2013 <> "Error*"EMA/655111/2013 \* MERGEFORMAT


In fact, looking at the XML, its a nested field (simplified and formatted):

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:p >
      <w:r >
        <w:fldChar w:fldCharType="begin"/>
                <w:instrText xml:space="preserve"> IF </w:instrText>
                <w:fldChar w:fldCharType="begin"/>
                        <w:instrText xml:space="preserve"> DOCPROPERTY "DM_emea_doc_ref_id"  \* MERGEFORMAT </w:instrText>
                <w:fldChar w:fldCharType="separate"/>
                        <w:instrText>EMA/655111/2013</w:instrText>
                <w:fldChar w:fldCharType="end"/>
                <w:instrText xml:space="preserve"> &lt;&gt; "Error*"</w:instrText>
                <w:fldChar w:fldCharType="begin"/>
                        <w:instrText xml:space="preserve"> DOCPROPERTY "DM_emea_doc_ref_id"  \* MERGEFORMAT </w:instrText>
                <w:fldChar w:fldCharType="separate"/>
                        <w:instrText>EMA/655111/2013</w:instrText>
                <w:fldChar w:fldCharType="end"/>
                <w:instrText xml:space="preserve"> \* MERGEFORMAT </w:instrText>
        <w:fldChar w:fldCharType="separate"/>
                <w:t>EMA/655111/2013</w:t>
        <w:fldChar w:fldCharType="end"/>
      </w:r >
 </w:p>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


docx4j doesn't have any specific support for IF fields, only for MERGEFIELD and DOCPROPERTY fields.

If your output documents will be opened in Word (as opposed to say, exported as PDF), I'd suggest you rely on Word to update the fields. You'll find manipulating this IF field effectively in docx4j challenging!

Re: Updating field display values in Word

PostPosted: Sat Jan 11, 2014 12:45 am
by andyflint
I agree that the IF field is the problem because if I create a new filed the field updater works fine.

I can resolve the problem with new documents by recreating the fields so that they are not wrapped in an {if} but for the existing documents I have a problem. I think that the options are

  • Delete and recreate the field
  • Update the property and field then search and replace the text

Have currently implemented the second option but this does present some issues in that I can't guarantee that the text I am replacing is unique to the field.

Is the delete and recreate the field do-able?