Leave the unmerged fields

Is there a way to leave unmerged field name in the Word document?

Hi
Olivier,

Thanks for your inquiry. I am afraid, I couldn’t understand your question. Please clarify, what do you mean by leaving unmerged field name in the Word document?

Best Regards,

It there an option that I can use to tell Aspose.words not to replace a null or blank value into a merged field so that they do not get replaced by a blank value? My users would like to see in the word document the merge fields intact when the data is blank or null, so they would know which ones did not have a value?

Hi,

Thanks for your inquiry. Sure, you can achieve this by implementing the IFieldMergingCallback Interface as follows:

private class HandleMergeFields : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldValue == null)
        {
            string fieldCode = e.Field.GetFieldCode().Trim();

            DocumentBuilder builder = new
            DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.FieldName);
            builder.InsertField(fieldCode);
        }
        else if (string.IsNullOrEmpty(e.FieldValue.ToString()))
        {
            string fieldCode = e.Field.GetFieldCode().Trim();

            DocumentBuilder builder = new
            DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.FieldName);
            builder.InsertField(fieldCode);
        }
        else
        {
            // Let the rest of the fields be merged
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    { }
}

I hope, this will help.

Best Regards,

It works perfectly. Thank you very much for your help.

Hi
Olivier,

Thanks for your feedback. Please let us know any time you have any further queries. We are always glad to help you.

Best Regards,

Hi
Olivier,

It is to update you that I have logged a new feature request in our bug tracking system as WORDSNET-6224 to address your original problem. Your request has also been linked to this issue and you will be notified as soon as it is supported. In the mean time you can use the work around code i.e. suggested in one of my previous posts.

Best Regards,

@olebigot,
Regarding WORDSNET-6224, our product team has completed the work on your issue and has come to a conclusion that they won’t be able to implement the fix to your issue. Your issue (WORDSNET-6224) has now been closed with ‘Won’t Fix’ resolution.
Aspose.Words leaves merge field unmerged when data source does not contain referenced column. The solution to leave merge field unmerged when field value is null is wrap original data source as follows:

public class MailMergeDataSource : IMailMergeDataSource
{
	private readonly IDictionary<string, object> mValues;
	private bool mIsMoved;

	public MailMergeDataSource(string[] fields, object[] values)
	{
		mValues = new Dictionary<string, object>(fields.Length, StringComparer.InvariantCultureIgnoreCase);
		for (int i = 0; i < fields.Length; i++)
			mValues[fields[i]] = values[i];
	}

	public string TableName
	{
		get { return null; }
	}

	public bool MoveNext()
	{
		if (mIsMoved)
			return false;

		mIsMoved = true;
		return true;
	}

	public bool GetValue(string fieldName, out object fieldValue)
	{
		return mValues.TryGetValue(fieldName, out fieldValue) && fieldValue != null; // <---
	}

	public IMailMergeDataSource GetChildDataSource(string tableName)
	{
		return null;
	}
}

Hope, this helps.
Best regards,