Page 1 of 1

changing mailmerge field's name using FieldLocator

PostPosted: Mon Apr 30, 2012 6:17 am
by arpit.gupta
Hello Friends,

I am working on mailmerge and for some reason i want to change the mailmerge field name by editing the document.

my mailmerge "FIELD_NAME" in document.xml looks like

<w:bookmarkStart w:id="0" w:name="_GoBack" />
<w:p w:rsidR="009C1457" w:rsidRDefault="00E2058F">
<w:r>
<w:fldChar w:fldCharType="begin" />
</w:r>
<w:r>
<w:instrText xml:space="preserve"> REF F121_1 \* MERGEFORMAT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate" />
</w:r>
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>«FIELD_NAME\»</w:t>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end" />
</w:r>
<w:r>
<w:fldChar w:fldCharType="begin" />
</w:r>
<w:r>
<w:instrText xml:space="preserve">SET F121_1 "\«FIELD_NAME\»" \* MERGEFORMAT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate" />
</w:r>
<w:bookmarkStart w:id="1" w:name="F121_1" />
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>«FIELD_NAME\»</w:t>
</w:r>
<w:bookmarkEnd w:id="1" />
<w:r>
<w:fldChar w:fldCharType="end" />
</w:r>
</w:p>
<w:bookmarkEnd w:id="0" />

Now using FieldLocator module i can easily list all my fields but i can not change the field names (shown red above)of these fields.
There are method available for traversing on a field and getting it's content like
//FieldRef fr
R r = fr.getBeginRun();
List<Object> objects = r.getContent();
but there isn't any method exposed for setting this content.

There is nothing available about FieldLocator module as it's not released yet. Is there any way to do this? or is there any other way to do this. i want to avoid a direct path search.

Thanks,
Arpit

Re: changing mailmerge field's name using FieldLocator

PostPosted: Mon Apr 30, 2012 10:05 am
by jason
Its w:instrText you want to change.

The FieldRef class contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public String getInstr() {
                return instrText.getValue().getValue();
        }
        public void setInstrText(JAXBElement<Text> instrText) {
                this.instrText = instrText;
        }
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


but instrText is not initialised in the constructor.

Have a look at the canonicalise method in the FieldsPreprocessor class to see how it can be done. Perhaps calling that method will do what you want.

Re: changing mailmerge field's name using FieldLocator

PostPosted: Mon Apr 30, 2012 6:19 pm
by arpit.gupta
Hi Jason,
Thanks for your response.

I already tried setting instrText but this will set the field text <w:t> which is marked green in above xml and not w:instrText.
I forgot to mention this in my question.

Re: changing mailmerge field's name using FieldLocator

PostPosted: Tue May 01, 2012 9:22 pm
by arpit.gupta
Hi jason, I tried to modify insrText as was in canonicalise method.

But still it doesn't change the the instrText value.

Code: Select all
else if (((o2 instanceof JAXBElement)) && (((JAXBElement)o2).getName().equals(_RInstrText_QNAME)))
         {
            Text text = new Text();
            text.setValue("New_Field_Name");
            JAXBElement<Text> element = new JAXBElement<Text>(_RInstrText_QNAME,Text.class, text);
            //element.getValue().setValue("New_Field_Name");
            //currentField.setInstrText((JAXBElement)o2);
            currentField.setInstrText(element);
            
            //newR.getContent().add(o2);
            newR.getContent().add(element);

            fieldRPr = existingRun.getRPr();
            newR.setRPr(fieldRPr);
         }

Any Help??

Thaks,
Arpit