Page 1 of 1

How to replace Mail Merge tags from word files

PostPosted: Wed Jan 21, 2009 3:50 pm
by binu
Hi I'm trying to find and replace mail merge tags in word document. I was able to replace simple mail merge tags by using regex pattern find and replace.
I read the document using docx4j api and did a find and replace for mail merge tags

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Document wmlDocumentEl1 = (Document) documentPart.getJaxbElement();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
// xml --> string
String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("SystemDate", "bla");
//mappings.put("PROP2", "bla bla");
// valorize template
Object obj = XmlUtils.unmarshallFromMailMergeTemplate(xml, mappings);
// change JaxbElement
documentPart.setJaxbElement(obj);
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
// save new documnt
saver.save(outPutFileName);


unmarshellfrom mailmerge code is as follows


Pattern patt = Pattern.compile("(<w:fldSimple w:instr=\" MERGEFIELD &quot;([a-zA-Z0-9_]+)&quot; \">(.*?)</w:fldSimple>)",Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);

Matcher m = patt.matcher(line);
StringBuffer sb = new StringBuffer(line.length());
while (m.find()) {
String mergeTag = m.group(1);
String mergeFieldName = m.group(2);
String value = mappings.get(mergeFieldName);
value ="<w:r><w:t>"+value+"</w:t></w:r>";
m.appendReplacement(sb, value);
}
m.appendTail(sb);
return sb.toString();


But some of the mail merge tags are not following above tag pattern , some follows a different pattern, Which i was not able to extract correctly, please let me know..
if u have any ideas .. Thanks

e.g

</w:p><w:p w:rsidRDefault="00B43014" w:rsidR="00B43014"><w:pPr>
<w:jc w:val="both"/></w:pPr></w:p><w:p w:rsidRDefault="00B43014" w:rsidR="00B43014"><w:pPr><w:jc w:val="both"/></w:pPr><w:r><w:t xml:space="preserve">Dear </w:t></w:r>
<w:bookmarkStart w:name="txtClmtName2" w:id="0"/><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText xml:space="preserve"> MERGEFIELD "EmployeeName" </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/>
</w:r><w:r><w:rPr><w:noProof/></w:rPr><w:t>«EmployeeName»</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r><w:r><w:t>:</w:t></w:r><w:bookmarkEnd w:id="0"/></w:p><w:p w:rsidRDefault="00B43014" w:rsidR="00B43014">
<w:pPr><w:jc w:val="both"/></w:pPr></w:p><w:p w:rsidRDefault="00B43014" w:rsidR="00B43014"><w:pPr><w:jc w:val="both"/></w:pPr><w:r>
<w:t xml:space="preserve">

Re: How to replace Mail Merge tags from word files

PostPosted: Wed Jan 21, 2009 4:12 pm
by binu
<w:fldSimple w:instr=" MERGEFIELD &quot;AdjusterPhoneExt&quot; "><w:r><w:rPr><w:noProof/></w:rPr><w:t>«AdjusterPhoneExt»</w:t></w:r></w:fldSimple>

I'm able to replace the above tags but not thoes one that starts with instrText

<w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText xml:space="preserve"> MERGEFIELD "AdjusterStartTime" </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r><w:rPr><w:noProof/></w:rPr><w:t>«AdjusterStartTime»</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/>