Free Support Forum - aspose.com

Suggestions on how best to create a Merge Field event handler much like the IMAGE: or TABLESTART:functionality?

Hello –

What I’d like to create is a variation on a merge field, much like the two special keywords IMAGE: and TABLESTART:, only I’d like to create one called CHECKBOX:.

In other words, I’d like my end users to be able to create a merge field that might look like this:
{ MERGEFIELD CHECKBOX:IsCitizen }

After performing the merge, what I’m hoping to achieve is to have Words insert a Checkbox Form Field at the location of the merge field, very much like the functionality of the IMAGE: tag, using DocumentBuilder.

However, I can’t just use the other examples in this forum that use a merge event handler to evaluate the data, looking for a boolean field, and using the builder to insert a checkbox because I need something that is generic enough for my end users to be able to choose to use, or not, as they wish.

The trouble I’m running into is that even in an event handler, if the merge field is named as I suggested above (including the word “Checkbox:”) the e.FieldValue won’t be the corresponding value from my data table.

Here’s what I started to write, but abandoned:

if (e.DocumentFieldName.StartsWith(“Checkbox:”, StringComparison.OrdinalIgnoreCase))
{
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToMergeField(e.DocumentFieldName);

// e.FieldValue won’t have a value though…
if (!String.IsNullOrEmpty(e.FieldValue.ToString()))
builder.InsertCheckBox(e.FieldName, true, 0);
else
builder.InsertCheckBox(e.FieldName, false, 0);
}

Do you have other suggestions on how I might accomplish this?

Hi

Thanks for your request. Try to map merge fields before execute mail merge. See the following code.

public void TestMailMerge_98142()

{

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

DataTable table = new DataTable();

table.Columns.Add("test");

DataRow row = table.NewRow();

row[0] = "blablabla";

table.Rows.Add(row);

string[] names = doc.MailMerge.GetFieldNames();

for (int i = 0; i < names.Length; i++)

{

if (names[i].StartsWith("Checkbox:", StringComparison.OrdinalIgnoreCase))

{

string name = names[i].Substring(9);

if (table.Columns.Contains(name))

{

doc.MailMerge.MappedDataFields.Add(names[i], name);

}

}

}

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_98142);

doc.MailMerge.Execute(table);

doc.Save(@"256_98142_DenverMike\out.doc");

}

void MailMerge_MergeField_98142(object sender, MergeFieldEventArgs e)

{

if (e.DocumentFieldName.StartsWith("Checkbox:", StringComparison.OrdinalIgnoreCase))

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.FieldName);

// e.FieldValue won't have a value though...

if (!String.IsNullOrEmpty(e.FieldValue.ToString()))

builder.InsertCheckBox(e.FieldName, true, 0);

else

builder.InsertCheckBox(e.FieldName, false, 0);

}

}

I have used a simple template for testing. It contains one mergefield.

<>

I hope that it will help you.

Best regards.

Hi Alexey,

This was EXACTLY what I was looking for! Thanks very much!

Mike