We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mailmerge number format switch

Hello,

we have a problem with formatting numbers during mailmerge. We get numbers with “.” as decimal separator from database but we need the result in french format (" " as thousands separator and “,” as decimal separator). I have tested almost every possible picture switch to achieve this but with no success. ("#,##0.00" or “# ##0,00” or " 0,00" or “,0.00” to name a few - the last one works great if I have english regional settings on my computer, if I switch it to french, it doesn’t work anymore. We need english and french formats because we are developing something for Canada)

Thank you,
Karel Bem

Hi Karel,

If MS Word switches do not help, you can implement IFieldMergingCallback to replace thousand separator (,) with space and decimal (.) with ',' as you can see in the following code. Please check http://www.aspose.com/docs/display/wordsnet/How+to++Apply+Custom+Formatting+during+Mail+Merge for more details.

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

{

static void Main(string[] args)

{

// Open an existing document.

Document doc = new Document("SimpleMerge.docx");

doc.MailMerge.FieldMergingCallback = new HandleMergeField();

doc.MailMerge.Execute(

new string[] { "Field1" },

new object[] { 100000.25 });

doc.Save("Aspose_Output.doc");

}

}

class HandleMergeField : IFieldMergingCallback

{

private DocumentBuilder mBuilder;

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(args.Document);

// This way we catch the beginning of a new row.

if (args.FieldName == "Field1")

{

mBuilder.MoveToMergeField(args.FieldName);

mBuilder.Write(args.FieldValue.ToString().Replace('.', ','));

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Do nothing.

}

}

Best Regards,

Thank you for your answer.


This is still not enough for us (I don’t know switch for " " as thousand separator) but we tried to use String.Format method + format of the number instead of picture switch and it is working for French format as well. But is there any way how to replace original value with formatted one in the callback method? I tried to use args.Field.Result = newValue but it throws NullReferenceException somewhere else in the code so I guess this is not allowed.

This is the code I have now:
//this method is called from IFieldMergingCallback.FieldMerging
private void FormatValue(FieldMergingArgs args)
{
var firstIndex = args.Field.GetFieldCode().IndexOf(’"’);
var lastIndex = args.Field.GetFieldCode().LastIndexOf(’"’) - 1;

if (firstIndex > -1 && lastIndex > -2)
{
var formatString = args.Field.GetFieldCode().Substring(firstIndex + 1, lastIndex - firstIndex);

args.Field.Result = String.Format(formatString, args.FieldValue);
}
}

Thank you.

Hi Karel,

We are working on this and will get back to you shortly.

Best Regards,

Hi Karel,

Following code worked at my end. Please try it and let us know if it works for you as well.

class Program

{

static void Main(string[] args)

{

try

{

License lic = new License();

lic.SetLicense("Aspose.Total.lic");

// Open an existing document.

Document doc = new Document("SimpleMerge.docx");

doc.MailMerge.FieldMergingCallback = new HandleMergeField();

doc.MailMerge.Execute(

new string[] { "Field1" },

new object[] { 100000.25 });

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

doc.Save("Aspose_Output.docx");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.WriteLine("Done");

Console.ReadKey();

}

}

class HandleMergeField : IFieldMergingCallback

{

private DocumentBuilder mBuilder;

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(args.Document);

// This way we catch the beginning of a new row.

if (args.FieldName == "Field1")

{

System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("fr-FR").NumberFormat;

double oldValue = Convert.ToDouble(args.FieldValue);

string updatedValue = oldValue.ToString("N", nfi);

mBuilder.MoveToMergeField(args.FieldName);

mBuilder.Write(updatedValue);

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Do nothing.

}

}

In this example, 'Field1' is a number field.

Best Regards,