Deleting Blank values in Mail Merge Fields

Hi,
I am looking for a solution to delete blank spaces arising due to Blank or null values in the mail merge fields.I got to see many posts in the similar lines.But, none of them seemed to provide a solution to the requirement i have.This is what I am required to display on a word document form a data source
<FirstName>,<LastName>,
<Organization>,
<AddressLine1>,
<AddressLine2>,
<City>,<State>,<Zip>
<Country>

If AddressLine2 is blank, I need <City><Zip> to be the next line.I tried doing this in the manner mentioned in
https://forum.aspose.com/t/109218
(Using MailMergeEventHandler)
Both of them didn’t work for me.In the latter one, text preceding the blank value also got deleted as the parent(which is the whole paragraph) is being removed

Kindly, let me know how this can be achieved.
Regards,
Pavithra V.S

Hi

Thanks for your request. I think you should specify “Text to be inserted before” or “Text to be inserted after” when you insert mergefield, in this case, coma after mergefield will be part of the mergefield. Please see the attached template and the following code:

// Create dummy datasource
string[] names = {
    "FirstName",
    "LastName",
    "Organization",
    "AddressLine1",
    "AddressLine2",
    "City",
    "State",
    "Zip",
    "Country"
};
object[] values = {
    "FirstName",
    "LastName",
    "Organization",
    "AddressLine1",
    "",
    "City",
    "State",
    "Zip",
    "Country"
};
// Open template
Document doc = new Document(@"Test190\in.doc");
// Specify optuion to remove empty paragraphs from the docuemnt
doc.MailMerge.RemoveEmptyParagraphs = true;
// exceute mail merge
doc.MailMerge.Execute(names, values);
// Save output doc
doc.Save(@"Test190\out.doc");

As you can see in the output document, AddressLine2 is completely removed, because there is no value.
Hope this helps.
Best regards.

Pavithra,
You have the exact same problem most of us have: Mail Merge is incomplete without the conditional fields. The way you would accomplish this in Microsoft Word is:
{ IF {MERGEFIELD address2} = "" "" "{ MERGEFIELD address2 }\n" }
Unfortunately, there’s no native support for conditional fields. You can try the option to remove blank lines, but that will only get you so far with a merge document, especially if you use the combination to define an address block.
I’m currently trying to write a quick and simple expression evaluator to get me around this limitation. I’ll try to check back here if I get it working.

Thank you for the qick replies.
As told earlier, it works for cases which are mapped like ‘names and values’ in the previous example.
setremoveemptyparagraphs doesn 't work in case of a datasource being the source of empty mail merge fields.
Kindly, keep me posted if your workaround resolves this problem

Thanks Again!
Pavithra

Hi Donald,

I answered your original request in the following thread:
https://forum.aspose.com/t/90893
Best regards.

Hi Pavithra,

Have you tried using code and template I provided in my previous post? I am sure, such approach will work in most of cases. So, if you create template yourself, or you can do modifications in the template, then this approach will work for you as well.
Best regards,

Hi Alexey,
Yes, I tried with the similar approach.That is what I said that it works when you are mapping values like that manually.
But, When the mailmerge field gets data from a merge data source, setremoveemptyparagraphs isn 't working. It worked for all the values mapped manually.

Regards,
Pavithra V.S

Hi

Thank you for additional information. Could you please attach your template here, output document and sample code? I will check the issue and provide you more information.
Best regards.