Free Support Forum - aspose.com

Mail Merge - merge fields

Hello there,

I am having a query regarding merge fields in Mail Merge.

If my Data Source has a property called ‘ClientAddress’,
is it possble that I simply add ‘ClientAddress’ as a text string in my template document?
Or it has to be a merge field?

Please guide.

Thank you!

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. No you cannot use text placeholders instead of merge fields during mail merge. You should use mail merge fields. Please see the following link to learn how to prepare document for mail merge:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/prepare-a-document.html

Best regards,

Thank you for the reply.

I have already gone through the link.
If I can not use text placeholders, then I will need a way to replace text placeholders with Merge Fields at run-time.

Is there any way I can add the merge fields at run-time in my document?

Thank you!

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. In your case, you can use code like the following to replace text placeholders with merge fields at run-time:

// Open document.

Document doc = new Document(@"Test055\in.doc");

Regex regex = new Regex(@"\[(?\S+)\]", RegexOptions.IgnoreCase);

// Find and replace placeholders

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceEvaluator), false);

// Save output document

doc.Save(@"Test055\out.doc");

====================================================================

private ReplaceAction ReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)

{

// Get name of placeholder.

string name = e.Match.Groups["name"].Value;

// If name is empty string, then do nothing.

if (string.IsNullOrEmpty(name))

return ReplaceAction.Skip;

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

Node currentNode = e.MatchNode;

// 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.MatchOffset > 0)

currentNode = SplitRun((Run)currentNode, e.MatchOffset);

// Create DocumentBuilder object, which will help us to insert filds.

DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);

// Move builder cursor to the current node.

builder.MoveTo(currentNode);

// Insert mergefield.

builder.InsertField(string.Format("MERGEFIELD {0}", name), string.Format("«{0}»", name));

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

ArrayList runs = new ArrayList();

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

int remainingLength = e.Match.Value.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.NextSibling;

}

while ((currentNode != null) && (currentNode.NodeType != 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);

}

// Now highlight all runs in the sequence.

foreach (Run run in runs)

run.Remove();

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

return ReplaceAction.Skip;

}

///

/// Splits text of the specified run into two runs.

/// Inserts the new run just after the specified run.

///

private static Run SplitRun(Run run, int position)

{

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

afterRun.Text = run.Text.Substring(position);

run.Text = run.Text.Substring(0, position);

run.ParentNode.InsertAfter(afterRun, run);

return afterRun;

}

I use placeholders like the following [PlaceholderName]

Also, I attached my input and output documents.

Hope this helps.

Best regards,