Mustache syntax doestn't work with dataset?

Hello,

we are trying to use mustache syntax for mail merge fields for the first time but it doesn’t work so far. We have dataset with one table and this table has 2 fields and I tried to just merge these fields into the document.

I attached the document template, xml of the dataset and the result file.

And there is piece of code that we use:

var myDoc = new Document(templateStream);

myDoc.MailMerge.FieldMergingCallback = new HandleMailMergeFields(highlightMailMergedParts);

if (removeEmptyParagraphs)
    myDoc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveEmptyParagraphs;

myDoc.MailMerge.UseNonMergeFields = true;
var xml = inputDataSet.GetXml(); // just for debugging
if (selectedTemplate.HasRegions)
{
    myDoc.MailMerge.ExecuteWithRegions(inputDataSet);
}
else
{
    myDoc.MailMerge.Execute(inputDataSet.Tables[0]);
}

myDoc.MailMerge.DeleteFields();

myDoc.UpdateFields();

I don’t know if it’s evem possible to mix Word merge field and these new fields.

I would appraciate any advice.

Hi Karel,

Thanks for your inquiry. Yes, you can mix mail merge fields with mail merge fields having ‘Mustache’ syntax. I have created two sample documents and have attached to this post for your kind reference. Please check the following code example.

Document doc = new Document(MyDir + "MailMergewithRegion.docx");
doc.MailMerge.UseNonMergeFields = true;
DataSet ds = new DataSet();
ds.ReadXml(MyDir + "dataset.xml");
doc.MailMerge.ExecuteWithRegions(ds);
doc.Save(MyDir + "MailMergewithRegion.Out.docx");
doc = new Document(MyDir + "SimpleMailMerge.docx");
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.Execute(new string[] { "ShortName"}, new string[] { "Short Name" });
doc.Save(MyDir + "SimpleMailMerge.Out.docx");

I suggest you please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/mail-merge-template-from-mustache-syntax/

Thank you,

we finally solved the issue, the problem was in code:

myDoc.MailMerge.FieldMergingCallback = new HandleMailMergeFields(highlightMailMergedParts);

Do you know if this is a known issue that this custom formatting and the new Mustache syntax cannot work together?

Thank you,
Karel Bem

Hi Karel,

Thanks for your inquiry.

*karel.bem:

we finally solved the issue, the problem was in code:
myDoc.MailMerge.FieldMergingCallback = new HandleMailMergeFields(highlightMailMergedParts);*

It is nice to hear from you that your problem has been solved. Yes, you achieve your requirement by implementing IFieldMergingCallback interface.

*karel.bem:

Do you know if this is a known issue that this custom formatting and the new Mustache syntax cannot work together?*

This is not an issue. Please check the code example and attached documents in my previous post. It would be great if you please share your input document along with sample code example to reproduce the issue which you are facing. We will then provide you more information about your query.

Thank you, we have solved the second issue as well (it was our custom code that needed to be changed)

And I have the last question (I hope), is there any way how to format field defined by this Mustache syntax. I know that I could format merge field in Word via * MERGEFORMAT option but is there something like this?

Thanks
Karel

Hi Karel,

Thanks for your inquiry. Yes, you can use mail merge formatting in Mustache syntax. Please check the following mail merge fields.

{ MERGEFIELD Test * Upper \b Start \f After * MERGEFORMAT }
{MERGEFIELD NumberValue # "#,##0.00"}

Mail Merge with Mustache syntax
{{ Test * Upper \b Start \f After * MERGEFORMAT }}
{{ NumberValue # "#,##0.00" }}

Hope this answers your query. Please let us know if you have any more queries.

Thank you for another advice.

I had no success yet but I think we are using this is much more complicated scenario.

We do merge twice, the first merge is just for merging the correct texts with Mustache syntax from database (users have for example 3 options which text will be on some place in the document and each of these 3 texts have Mustache syntax field in it so the first option could be something like "Hello {{ ClientName}}", second option could "Dear {{ ClientShortName}} etc.)

If I try to write format into field like {{PremiumEstimated # “#,#0.00” }}, the result is always something like MERGEFIELD "PremiumEstimated # “##0.00"” - so there are too many quotes and value is not merged at all.

Is there something I could try?

Thanks
Karel Bem

Hi Karel,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.10.0) from here and let us know how it goes on your side. Perhaps, you are not using the correct field name while doing mail merge. If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

Hi,

we tried new version but the problem remains.

Original field is {{PremiumEstimated # "#,##0.00" }}
Field after merge (in IFieldMergingCallback.FieldMerging method) is MERGEFIELD "PremiumEstimated # "#,##0.00"" and its value is null (it is not recognized)

I don’t understand why additional quotes are add at the beginning and at the end of the field code.

The only “hack” which helped was adding another quote manually at the end of field name so the original merge field looked like {{PremiumEstimated" # "#,##0.00" }}

Thank you.
Karel Bem

Hi Karel,

Thanks
for your inquiry. I have created a template document with following two mail merge fields. Please check the attached documents.

{{ PremiumEstimated1 # "#,##0.00"}}
{{ PremiumEstimated2 # "#,##0.00"}}

Following code example perform the mail merge twice. In IFieldMergingCallback.FieldMerging the field codes are as follow. There is no issue while performing mail merge.

MERGEFIELD "PremiumEstimated1" # "#,##0.00"
MERGEFIELD "PremiumEstimated2" # "#,##0.00"

Please note that if your implementation does not perform merge merge for all fields, the mail merge fields which are not merged are convert to normal merge fields in output document. For example, if there is another mail merge field named PremiumEstimated3 and this field is not merged, the output document will have «PremiumEstimated3» field with field code { MERGEFIELD "PremiumEstimated2" # "#,##0.00" }.

Document doc = new Document(MyDir + "Test40.docx");
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldTest40();
doc.MailMerge.Execute(new string[] { "PremiumEstimated1" }, new object[] { 123456 });
doc.MailMerge.Execute(new string[] { "PremiumEstimated2" }, new object[] { 123456 });
doc.Save(MyDir + "Out.docx");
private class HandleMergeFieldTest40 : IFieldMergingCallback
{
    /// 
    /// This is called when merge field is actually merged with data in the document.
    /// 
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldValue != null)
        {
            Console.WriteLine(e.Field.GetFieldCode() + " : " + e.FieldValue);
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.DocumentFieldName);
            builder.Write(e.FieldValue.ToString());
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}

Hope this answers your query. If you still face problem, please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please create a standalone/runnable simple application (for example a Console Application Project) that demonstrates the code (Aspose.Words code) you used to generate your output document
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document.

I will investigate as to how you are expecting your final document be generated like.

Thank you,

I tried version 14.10 and it finally worked (it didn’t work with 14.9)

So thank you for all your help!

Best regards,
Karel Bem

Hi Karel,

Thanks for your feedback. It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.