Page 1 of 1

MailMerger Class Bugs and Usage

PostPosted: Wed Apr 18, 2012 3:42 am
by CesarDRK
I downloaded the latest version of the source code and have been trying to use the MailMerger class with a docx containing MailMerge fields.

First, it didn´t work, had to fix the line 268/269 from MailMerger.java

from:
Code: Select all
tmp = tmp.trim();
String key  = tmp.substring(0, tmp.indexOf(" ")) ;


to:
Code: Select all
String key  = tmp.trim();


Why the substring from 0 to " " ? The line before it does the trick, with the trim()..

Second, now it works almost perfectly !! But when i open the generated docx it still comes with the datasource reference, like "the following SQL command will be executed when this document blablabla", even tough if i say no the document opens OK with all the fields replaced correctly...

Any idea on how to remove this datasource reference so the result will look like a regular word document??

Thanks in advance! :mrgreen:

Re: MailMerger Class Bugs and Usage

PostPosted: Thu Apr 19, 2012 10:43 pm
by jason
Re the datasource reference, I suspect that must be in some part which is included in your docx.

If you can attach a docx which exhibits the issue (you can delete the text contents in the main document part), i ought to be able to find it quickly.

cheers .. Jason

Re: MailMerger Class Bugs and Usage

PostPosted: Fri Apr 20, 2012 12:18 am
by CesarDRK
jason wrote:Re the datasource reference, I suspect that must be in some part which is included in your docx.

If you can attach a docx which exhibits the issue (you can delete the text contents in the main document part), i ought to be able to find it quickly.

cheers .. Jason


Here´s the file.

By the way, i´ve added the 2 following lines at the end of the perform method in MailMerger.java, and it worked for me.
But i don´t know if this is the correct way to remove the references...

Code: Select all
//Delete MailMerge Data Source Part.
DocumentSettingsPart settingsPart = ((MainDocumentPart)contentList).getDocumentSettingsPart();
settingsPart.getJaxbElement().setMailMerge(null);


Thanks.

Re: MailMerger Class Bugs and Usage

PostPosted: Sat Apr 28, 2012 7:21 am
by CesarDRK
Jason, any updates? Really looking foward to this fixed in the 2.8.0 release !

Thanks.

Re: MailMerger Class Bugs and Usage

PostPosted: Mon Apr 30, 2012 10:54 am
by jason
CesarDRK wrote:Why the substring from 0 to " " ? The line before it does the trick, with the trim()..


In terms of the example in the code comment:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:instrText xml:space="preserve"> MERGEFIELD  Kundenstrasse \* MERGEFORMAT </w:instrText>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


The line:
Code: Select all
               
               String tmp = instr.substring( instr.indexOf("MERGEFIELD") + 10);

gets the stuff after MERGEFIELD.

Code: Select all
               tmp = tmp.trim();


trims any leading spaces; it will also trim trailing ones, but we don't care about that, given the following line:

Code: Select all
               String key  = tmp.substring(0, tmp.indexOf(" ")) ;


which gives you the key name, eg Kundenstrasse as opposed to Kundenstrasse \* MERGEFORMAT

What does the XML it was failing on for you look like?

cheers .. Jason

Re: MailMerger Class Bugs and Usage

PostPosted: Thu May 03, 2012 2:03 am
by CesarDRK
Now is see, my fields don´t have the "MERGEFORMAT" in the end, they look like this:

Code: Select all
<w:instrText xml:space="preserve"> MERGEFIELD Field_Name </w:instrText>

Re: MailMerger Class Bugs and Usage

PostPosted: Tue May 22, 2012 4:55 pm
by jason
OK, fixed in https://github.com/plutext/docx4j/commi ... erger.java

Didn't make it into last night's nightly build, but will be in 2.8.0 release (probably next week).

cheers .. Jason