We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

RemoveEmptyParagraphs setting not working

I have a simple document that I am using for mail merge purposes with 2 fields on it

One of the fields is in a paragraph on its own and and isnt used for mail merging purposes. So setting RemoveEmptyParagraphs to true, should remove this paragraph

However I find that that when I saved the merged document, the paragraph containing the merged field still remains, and so I have an empty line.

I have checked the document I am using for mailmerging and there is no empty tabs, or spaces around the merge field, so what else can be casuing this paragraph to not be removed?

Many thanks

Hi,

Please attach the document to test.

Attached is the document

Its Field2a thats the problem

Heres the code I used:

string tempate = @"Test2.doc";
string destinationFileName = @"test2 Merged.doc";

Document doc = new Document(tempate);

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Field1", typeof(string)));
dt.Columns.Add(new DataColumn("Field2", typeof(string)));
dt.Rows.Add(new object[] { "Field1Data", "Field2Data" });

doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.Execute(dt);
doc.MailMerge.DeleteFields();

doc.Save(destinationFileName);

Many thanks

RemoveEmptyParagraphs actually only removes paragraphs contained merge fields which have been filled with an empty data such as null. DeleteFields removes retained merge fields but does not remove their parent paragraphs. So you should run the following code after mail merge to clean up the document:

NodeList fields = doc.SelectNodes("//FieldStart");

foreach (FieldStart field in fields)

{

if (field.FieldType == FieldType.FieldMergeField)

field.ParentNode.Remove();

}

We will consider implementing a special DeleteFields overload that encapsulates this functionality; I've logged it as issue #1394.

I am a bit confused. I am using VB.Net. The following code worked with the Aspose.Words version about a year ago:

doc.MailMerge.RemoveEmptyParagraphs = True
doc.MailMerge.Execute(fieldNames, fieldValues)

The above code removed merge fields that were on a line with only that merge field, and if there was no data for it.

Now with the latest version of Aspose.Words it looks like I need to do the following:

doc.MailMerge.RemoveEmptyParagraphs = True
doc.MailMerge.Execute(fieldNames, fieldValues)
doc.MailMerge.DeleteFields()

But that does not even work. Using the C# code from the previous post, it also looks like I have to do this:

NodeList fields = doc.SelectNodes("//FieldStart");

foreach (FieldStart field in fields)
{
if (field.FieldType == FieldType.FieldMergeField)
field.ParentNode.Remove();
}

So what is going on? What is the best way to remove all lines that only have the merge field, and the data for the merge field is null.

Derek

Actually, I think RemoveEmptyParagraphs is simply not working. Can you test this in the latest version?

Derek

OK, I am going to answer my own question here. I am using the following Aspose command:

doc.MailMerge.RemoveEmptyParagraphs = True
doc.MailMerge.Execute(fieldNames, fieldValues)

If a version of Aspose.Words from a year ago, if an array value in fieldvalues was Nothing, then Aspose would treat this as blank data and remove the merge field and entire row in the document. So I upgraded to the latest version of Aspose, and found that the merge fields were not disappearing. I have to, in my code, specifically set the array value to the empty string in fieldvalues is there is no data associated with that merged item. So before it was acceptible to have nothing, and now it needs to be the empty string.

Derek

Hi Derek,

I have tested the behavior with the latest version of Aspose.Words. You are correct, null (or Nothing) does not remove merge fields, whereas empty strings are considered a valid input and do replace the fields. Honestly, I don't remember what behavior was there an year ago. Anyway, currently you should either use empty strings in conjunction with RemoveEmptyParagraphs or additionally clean up the retained merge fields using the above approach.

The issues you have found earlier (filed as WORDSNET-979) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)