Free Support Forum - aspose.com

Trim Field In Mail Merge

I am mail merging some data and it has leading spaces. I want to trim the spaces.

I'm have a doc |<>|<>|<>|

I'm getting |001| 6|001| which is correct since the 6 has 2 leading spaces. I'd like to have it come out |001| 6|001|

I can't seem to find a setting that would trim Field2. Unfotunatly I have no control of the data so I can't trim it before the mail merge.

Thanks

Jim


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

Thanks for your inquiry. You can easily achieve this using MergeField event handler. Please see the following link for more information:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.reporting.mergefieldeventhandler.html

Also here a simple code, which you can use:

// Open template.

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

// Add MergeField event handler.

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

// Execute mail merge.

doc.MailMerge.Execute(new string[] { "field1", "field2", "field3" }, new object[] { " 123", "123 ", " 123 " });

// Save output document.

doc.Save(@"Test001\out.doc");

=============================================================

void MailMergeTrimValues(object sender, MergeFieldEventArgs e)

{

// Trim the value before insertign it into the document.

e.Text = e.FieldValue.ToString().Trim();

}

Hope this helps.

Best regards.

This is exactly what I needed. Thanks for the quick response!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />