Mailing labels with comma print empty columns with comma

Hi,
We are evaluating the aspose with our product. We have the Mailing labels with comma’s in between address1, address2 and city. When we merge the datatable, it merges fine. But empty columns are filled with comma’s. Even though the code has the
doc.MailMerge.RemoveEmptyParagraphs = true; before the mail merge execution
and
doc.MailMerge.DeleteFields(); after the mail merge execution.
Here the sample mail merge label format

<<LastName>> , <<FirstName>>
<<Address1>>, <<Address2>>, <<City>>

Could you please help to fix this issue.
–Raj

Hi
Thanks for your inquiry. Empty fields are not filled with comma. There is comma between fields in your template so if field have no data you will get the following result.
For example Addres2 is empty
LastName , FirstName
Address1, , City
I think you should use “Text to be inserted after” MergeField property as shown in the attached document. Here is code I used for testing.

// Open document
Document doc = new Document(@"Test101\in.doc");
// Execute mail merge
doc.MailMerge.Execute(new string[] { "test", "test1" }, new object[] { "", "value" });
// Save document
doc.Save(@"Test101\out.doc");

Best regards.

Hi
Thanks for your reply. But we have the custom mailing labels also. Customers can create and upload the word documents. When you merge just 2 records, we get 2 labels as well as empty labels with just comma’s. We want to delete the rest of the empty labels which has just comma’s. Since datatable has just 2 records, we were expecting just 2 labels without any other empty labels.
Ms-Word merge works fine with these type of scenario. But Aspose produce the empty labels with comma’s.
-Thanks
Raj

Hi
Thanks for your request. Could you please attach your template for testing? Also please attach output documents generated by MS Word and Aspose.Words. I will investigate the problem and provide you more information.
Best regards.

Hi,
Thanks for your reply. Here i have attached the merge template and result of AsposeMerge and MsWord merge.
Thanks
–Raj

Hi,
Additional info for you.
Here the which we use…

doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.UseNonMergeFields = false;
doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeFieldEvent);
doc.MailMerge.Execute(dt);
doc.MailMerge.DeleteFields();

and this is the event

private void MailMerge_MergeFieldEvent(object sender, MergeFieldEventArgs e)
{
    // avoid the mergefield, if the data is empty
    if (e.FieldValue == null || e.FieldValue.ToString().Trim() == "")
        e.Text = string.Empty;
}

Hope this will help you identify the issue.
–Raj

Hi
Thank you for additional information. You can try using the following code as a workaround.

doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.UseNonMergeFields = false;
doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeFieldEvent);
doc.MailMerge.Execute(dt);
// Get collection of field Starts
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
ArrayList cellsList = new ArrayList();
foreach (FieldStart start in starts)
{
    if (start.FieldType == FieldType.FieldMergeField)
    {
        Cell cell = start.GetAncestor(NodeType.Cell) as Cell;
        if (cell != null && !cellsList.Contains(cell))
            cellsList.Add(cell);
    }
}
// Remove content from cells without values
foreach (Cell cell in cellsList)
{
    cell.RemoveAllChildren();
}
doc.MailMerge.DeleteFields();

Also, I saw that there is IF fields in your template. Note that Aspose.Words does not evaluate IF fields. See FAQ from more information.
Best regards.