Free Support Forum - aspose.com

Issue On Mail Merging

Hi All,
I am mail merging using ASPOSE WORD. I am having an issue during the merging. Let me explain with an example.
These are the merge fields in the template (.dot).

<>
<>
<>
<>
<>
<>

While merging, there are possibilities that some of the fields from the database can be null or empty. So the current output is as follows.


Jaya
LINDA C. COFFEE

36 TWP RD 1098


CHESAPEAKE, OH 45619
US

Issue: You can notice an empty line between the address due to the null value of <>. I WANT TO SUPPRESS THAT EMPTY LINE. The desired output is as follows.

Jaya
LINDA C. COFFEE

36 TWP RD 1098
CHESAPEAKE, OH 45619
US


Please let me know how to solve this issue? Expecting for you reply.

Thank you

Thiru.

Hi

Thank you for your interest in Aspose.Words. I think that you should use MailMerge.RemoveEmptyParagraphs. This property specifies whether paragraphs that contained mail merge fields with no data should be removed from the document. For example see the following code.

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

string[] names = { "Name", "addressline1", "addressline2", "addressline3", "addressline4", "addressline5" };

string[] values = { "Jaya", "LINDA C. COFFEE", "36 TWP RD 1098", "", "CHESAPEAKE, OH 45619", "US" };

doc.MailMerge.RemoveEmptyParagraphs = true;

doc.MailMerge.Execute(names, values);

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

I hope this could help you.

Best regards.

Hi Alexey…

Thanks a lot… It worked for me… :slight_smile:

Hi Alexey…

I am having around 250 templates for mail merging. The solution u gave for removing the empty spaces works for some of the tempaltes and it doesn’t works for some other templates. Any suggestion regarding this issue will be helpful for me. Here with i attach a document template.
In the attached document template i had highlighted the fields which receives empty values from database.
Based on my observation the merge fields which have a empty space before it, is not suppressing the row and other fields are suppressing the row. So is there any way to suppress this kind of fields?


Hi

Thanks fro your inquiry. This occurs because this paragraph is not empty. There are a couple of white space symbols. As workaround you can try using the following code.

public void Test024()

{

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

string[] names = doc.MailMerge.GetFieldNames();

string[] values = (string[])names.Clone();

for (int i = 0; i < values.Length; i++)

{

values[i] = "";

}

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

doc.MailMerge.Execute(names, values);

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

}

void MailMerge_MergeField_024(object sender, MergeFieldEventArgs e)

{

if (string.IsNullOrEmpty(e.FieldValue.ToString()))

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.FieldName);

//Get Paragraph text

string parText = builder.CurrentParagraph.ToTxt().Replace(" ", "");

if (parText == "\r\n")

{

//If paragraph is empty then remove it.

builder.CurrentParagraph.Remove();

}

}

}

I hope this could help you.

Best regards.

Hi alexey

It worked for me… U help me at a critical suitation…

Thanks a lot…

Thiru