We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Cutstom MailMerge Tags

Hi,


I’m wondering if it possible to define a set of custom Mail Merge items.
I understand that Aspose.words can make use of the standard mail-merge tags already defined in the the MS Word.

We have Additional data available to use from our Business Objects that we would like to add as a a Mail Merge Tag. Does Aspose.Words provide this functionality

Thanks

Hi there,

Thanks for your inquiry. Could you please attach your input Word document which contains the custom tags and expected output document here for our reference? We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.


Hi


I’ve attached two documents one a sample template and one the expected results

Thanks
Kevin
Hi Kevin,

Thanks for sharing the documents. You can achieve your requirement by implementing IReplacingCallback interface. Please use the following code example to find the custom tags e.g and replace with some values. Read the following documentation link for your kind reference.
http://www.aspose.com/docs/display/wordsjava/Find+and+Replace+Overview

Hope this helps you. Please let us know if you have any more queries.


Document doc = new Document(MyDir + "SampleLetterTemplate.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

doc.getRange().replace(Pattern.compile("", Pattern.CASE_INSENSITIVE), new ReplaceEvaluator ("James", builder), false);

doc.getRange().replace(Pattern.compile("", Pattern.CASE_INSENSITIVE), new ReplaceEvaluator ("Bond", builder), true);

doc.getRange().replace(Pattern.compile("", Pattern.CASE_INSENSITIVE), new ReplaceEvaluator ("Milbank", builder), true);

doc.save(MyDir + "Out.docx");

public class ReplaceEvaluator implements IReplacingCallback

{

String strRepalce;

DocumentBuilder builder;

public ReplaceEvaluator (String repalcevalue, DocumentBuilder dbuilder)

{

strRepalce = repalcevalue;

builder = dbuilder;

}

public int replacing(ReplacingArgs e) throws Exception {

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.getMatchNode();

// The first (and may be the only) run can contain text before the match,

// in this case it is necessary to split the run.

if (e.getMatchOffset() > 0)

currentNode = splitRun((Run)currentNode, e.getMatchOffset());

// This array is used to store all nodes of the match for further highlighting.

ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.

int remainingLength = e.getMatch().group().length();

while (

(remainingLength > 0) &&

(currentNode != null) &&

(currentNode.getText().length() <= remainingLength))

{

runs.add(currentNode);

remainingLength = remainingLength - currentNode.getText().length();

// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.

do

{

currentNode = currentNode.getNextSibling();

}

while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));

}

// Split the last run that contains the match if there is any text left.

if ((currentNode != null) && (remainingLength > 0))

{

splitRun((Run)currentNode, remainingLength);

runs.add(currentNode);

}

builder.moveTo((Run)runs.get(0));

builder.write(strRepalce);

for (Run run : (Iterable) runs){

run.remove();

}

// Signal to the replace engine to do nothing because we have already done all what we wanted.

return ReplaceAction.SKIP;

}

private Run splitRun(Run run, int position) throws Exception

{

Run afterRun = (Run)run.deepClone(true);

afterRun.setText(run.getText().substring(position));

run.setText(run.getText().substring((0), (0) + (position)));

run.getParentNode().insertAfter(afterRun, run);

return afterRun;

}

}