I am mail merging some data and it has leading spaces. I want to trim the spaces.
I’m have a doc |<<field1>>|<<field2>>|<<Field3>>|
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
Thanks for your inquiry. You can easily achieve this using MergeField event handler. Please see the following link for more information:
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
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!