MailMerge and Word Check Box Form Field Problem

Hi,

I am attempting to perform a Mail Merge using Aspose.Words and would like to know the following:

I have several Check Box Form Fileds in a Microsoft Word document. The checkboxes get get checked on or off by clicking on them. I need these Check Boxes to act like Merge Fields so that when I perform a Mail Merge, they will have their value set to On (Checked), or Off (UnChecked)? In essence, make the Check Boxes behave and like Merge Fields.

It would be nice to have this functionality while doing the Mail Merge as it automatic and does not required extra coding or checking. Currently, when I view the properties of the Word CheckBox Form Field, I cannot assign any type of Name to it. Any help on this?

Regards,

Giovanni

At the moment Aspose.Words does not treat form fields as merge fields when populating a document with data.

Normally, to use Aspose.Words’ mail merge engine you need to have MERGEFIELD fields in the document. However, due to special customers’ requests Aspose.Words also supports MACROBUTTON and a special form of IF fields (IF 0 = 0 “{PatientsNameFML}” “”) as mail merge fields. So it is a possibility that we implement this (using form fields as merge fields), but we are overwhelmed with work on many new features already and I will have to queue this one up.

So it would be nice if you find a way to proceed without waiting for this feature. I see several workarounds that can be done to achieve what you want and there could be more:

  1. Do not use check box form fields. Insert normal MERGEFIELD into the template document. Implement a custom mail merge handler and when you handle a boolean field, insert an appropriate empty box or a box with a tick Windging character. In the final document you will get a character that looks like a checkbox but ain’t a checkbox. Maybe its better than the checkbox because it will not be clickable and switchable.

  2. Insert normal MERGEFIELD into the template document. Implement a custom mail merge handler. Inside the mail merge handler, when processing a boolean field, use DocumentBuilder.InsertCheckBox to actually insert a checkbox and set it checker or unchecked.

If you cannot have MERGEFIELD in the template document and stuck with check box form fields, then you can write code that will find and replace check box form fields (nodes) with MERGEFIELD fields and then you can execute mail merge with the option 1. or 2. above.

Although you wanted a solution without extra coding - you can see it is not possible right now. I will log a feature request, but no promise of implementation for it yet. As you can see Aspose.Words is pretty flexible and there is almost now limit in what you can do with a Word document using Aspose.Words with a bit of coding. I’ve given some of the ideas that come to mind how you can proceed. If you have a difficulty with anything or need sample code, let us know.

You can check the following thread where the similar question was discussed:
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback

Please try the code given there and let me know if it fits your task.

Best regards,

Hi,

The sample seems to be for checkboxes; my specific issue is that they are Form Field Check boxes. I don’t know if regular Checkboxes and Form Field Checkboxes are the same and if the same technique can be applied. I simply created a new document and added a CheckBox Form Field (View -> Toolbars -> Forms -> CheckBox Form Field), NOT a regular Checkbox. Any ideas? or samples?

Regards,

Giovanni

Attached is a demo project for this scenario. It shows how to insert check boxes and text input form fields during mail merge using mail merge event handler in Aspose.Words.

I’m pasting the code for quick reference:

using System;
using System.Data;
using System.IO;
using System.Reflection;
using Aspose.Words;
using Aspose.Words.Reporting;

namespace Project
{
    /// 
    /// This sample shows how to insert check boxes and text input form fields during mail merge into a document.
    /// 
    class Demo
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main(string[] args)
        {
            Demo demo = new Demo();
            demo.Execute();
        }

        private void Execute()
        {
            // Get the bin directory.
            string binDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            // The templates are stored in the directory one level up.
            string templateDir = Path.GetDirectoryName(binDir);

            // Load the template document.
            Document doc = new Document(Path.Combine(templateDir, "Template.doc"));

            // Setup mail merge event handler to do the custom work.
            doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);

            // The mail merge event handler will use this document builder object to do its work.
            mBuilder = new DocumentBuilder(doc);

            // Get the data for mail merge.
            DataTable table = GetDataTable();

            // Execute the mail merge.
            doc.MailMerge.Execute(table);

            // Save the finished document.
            doc.Save(Path.Combine(templateDir, "Template Out.doc"));
        }

        private DataTable GetDataTable()
        {
            // We create a hardcoded table here. In the real application you would get the data from a database.
            DataTable table = new DataTable();

            // Create some columns in the data table.
            table.Columns.Add("RecipientName", typeof(string));
            table.Columns.Add("SenderName", typeof(string));
            table.Columns.Add("FaxNumber", typeof(string));
            table.Columns.Add("PhoneNumber", typeof(string));
            table.Columns.Add("Subject", typeof(string));
            table.Columns.Add("Body", typeof(string));
            table.Columns.Add("Urgent", typeof(bool));
            table.Columns.Add("ForReview", typeof(bool));
            table.Columns.Add("PleaseComment", typeof(bool));

            // Add some rows.
            table.Rows.Add(new object[] { "Josh", "Jenny", "123456789", "", "Hello", "Test message 1", true, false, true });
            table.Rows.Add(new object[] { "Bob", "Jenny", "444 327589", "", "Bye", "Test message 2", false, true, false });

            return table;
        }

        /// 
        /// This handler is called for every mail merge field found in the document,
        /// for every record found in the data source.
        /// 
        private void HandleMergeField(object sender, MergeFieldEventArgs e)
        {
            // We decided that we want all boolean values to be output as check box form fields.
            if (e.FieldValue is bool)
            {
                // Move the "cursor" to the current merge field.
                mBuilder.MoveToMergeField(e.FieldName);

                // It is nice to give names to check boxes. Lets generate a name such as MyField21 or so.
                string checkBoxName = string.Format("{0}{1}", e.FieldName, e.RecordIndex);

                // Insert a check box.
                mBuilder.InsertCheckBox(checkBoxName, (bool)e.FieldValue, 0);

                // Nothing else to do for this field.
                return;
            }

            // Another example, we want the Subject field to come out as text input form field.
            if (e.FieldName == "Subject")
            {
                mBuilder.MoveToMergeField(e.FieldName);
                string textInputName = string.Format("{0}{1}", e.FieldName, e.RecordIndex);
                mBuilder.InsertTextInput(textInputName, TextFormFieldType.RegularText, "", (string)e.FieldValue, 0);
            }
        }

        private DocumentBuilder mBuilder;
    }
}