Page 1 of 1

exception occurred when using MailMerger w VariablePrepare

PostPosted: Wed May 22, 2013 5:48 pm
by raylapse
Hi Jason,

I just tried a very simple case to use MailMerger but encountered an exception which I can not understand.
Basically in my template I have 3 merge fields. When I author this template, I switch on the spellcheck and rsid in word, because I want to test if the MailMerger still works in this case.
The result is, if I call VariablePrepare before calling MailMerger, there is always an nullpointer exception from FieldRef.
If I don't call VariablePrepare but call MailMerger directly, the merge result is unexpected - some dirty texts remains after the merged field.

Am I doing something wrong or it's a limitation of VariablePrepare/MailMerger? Please suggest.

Code: Select all
/**
*
*/
package local.test;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.docx4j.model.datastorage.migration.VariablePrepare;
import org.docx4j.model.fields.merge.DataFieldName;
import org.docx4j.model.fields.merge.MailMerger;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

/**
* @author I034206
*
*/
public class MyTest {

   public static void main(String[] args) {
      WordprocessingMLPackage docML = null;

      try {
         docML = WordprocessingMLPackage.load(new FileInputStream(new File("C:/ide/m0.docx")));

         VariablePrepare.prepare(docML);

         List<Map<DataFieldName, String>> data = new ArrayList<Map<DataFieldName, String>>();
         Map<DataFieldName, String> map = new HashMap<DataFieldName, String>();
         map.put(new DataFieldName("bb_name"), "mybbname");
         map.put(new DataFieldName("bb_id"), "mybbid");
         map.put(new DataFieldName("activity_desc"), "myactivity");
         data.add(map);

         MailMerger.performMerge(docML, map, true);

         docML.save(new File("C:/ide/result.docx"));

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}


Code: Select all
java.lang.NullPointerException
   at org.docx4j.model.fields.FieldRef.getInstr(FieldRef.java:311)
   at org.docx4j.model.fields.merge.MailMerger.performOnInstance(MailMerger.java:468)
   at org.docx4j.model.fields.merge.MailMerger.performMerge(MailMerger.java:365)
   at local.test.MyTest.main(MyTest.java:39)


Thanks in advance & Best Regards,
Daniel

Re: exception occurred when using MailMerger w VariablePrepa

PostPosted: Wed May 22, 2013 6:15 pm
by jason
VariablePrepare is not intended for use in conjunction with MailMerger, and from your description it sounds like it is cleaning the document in a way which doesn't work with MERGEFIELDs.

If MailMerger is not working properly, we have to look into why that is. Don't compound the problem with VariablePrepare.

Re: exception occurred when using MailMerger w VariablePrepa

PostPosted: Wed May 22, 2013 7:20 pm
by raylapse
Hi Jason,

From the comments at the beginning of VariablePrepare, I original thought it supports fields as well:) Sorry for my poor English.
OK, then if we focus the question to MailMerger, My testing result is like below:
Before merge:
Code: Select all
      <w:r w:rsidR="009123E5">
         <w:rPr>
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:fldChar w:fldCharType="begin" />
      </w:r>
      <w:r w:rsidR="009123E5">
         <w:rPr>
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:instrText xml:space="preserve"> MERGEFIELD  bb_id  \* MERGEFORMAT </w:instrText>
      </w:r>
      <w:r w:rsidR="009123E5">
         <w:rPr>
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:fldChar w:fldCharType="separate" />
      </w:r>
      <w:r w:rsidR="009123E5">
         <w:rPr>
            <w:noProof />
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:t>«</w:t>
      </w:r>
      <w:proofErr w:type="spellStart" />
      <w:r w:rsidR="009123E5">
         <w:rPr>
            <w:noProof />
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:t>bb_id</w:t>
      </w:r>
      <w:proofErr w:type="spellEnd" />
      <w:r w:rsidR="009123E5">
         <w:rPr>
            <w:noProof />
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:t>»</w:t>
      </w:r>
      <w:r w:rsidR="009123E5">
         <w:rPr>
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:fldChar w:fldCharType="end" />
      </w:r>


After merge:
Code: Select all
      <w:r>
         <w:rPr>
            <w:noProof />
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:t>mybbid</w:t>
      </w:r>
      <w:proofErr w:type="spellStart" />
      <w:r>
         <w:rPr>
            <w:noProof />
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:t>bb_id</w:t>
      </w:r>
      <w:proofErr w:type="spellEnd" />
      <w:r>
         <w:rPr>
            <w:noProof />
            <w:lang w:val="en-US" />
         </w:rPr>
         <w:t>»</w:t>
      </w:r>


The merge filed is <bb_id>, I want to replace it to "mybbid".
However, you see the actual result is "mybbidbb_id>".

Since the field <bb_id> is splitted to several runs due to rsid and spellchecks, the MailMerger seems only replace the first run of the field.
I am already using the 3.0.0alpha build. So currently I have no idea how to solve this problem? Turning off rsid and spellcheck in word would not an option to me, because the templates would authored by various users in my orgnization - I don't think asking them to do that is an easy approach.

Any suggestion to overcome this issue or did you already have a plan to improve the MailMerger?

Best Regards, Daniel

Re: exception occurred when using MailMerger w VariablePrepa

PostPosted: Thu May 23, 2013 5:28 pm
by jason
Fixed by https://github.com/plutext/docx4j/commi ... 0afa0d885e

I'll create a nightly build tonight.