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

Free Support Forum - aspose.com

Problem with IF statement within regions

Here is a snippet of the Word template...

<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />{ MERGEFIELD TableStart:Level1 }
{ IF { MERGEFIELD VList } = "Yes" "{ MERGEFIELD TableStart:Level2 } {MERGEFIELD TextF3 \!format "html:TextF3" \* MERGEFORMAT } { MERGEFIELD TableEnd:Level2 }" "No records to report" } }
{ MERGEFIELD TableEnd:Level1 }
{ MERGEFIELD TableStart:Level1 }
{ IF { MERGEFIELD VList } = "Yes" "{ MERGEFIELD TableStart:Level2 } {MERGEFIELD TextF3 \!format "html:TextF3" \* MERGEFORMAT } { MERGEFIELD TableEnd:Level2 }" "No records to report" } }
{ MERGEFIELD TableEnd:Level1 }

VList is a field in the Level1 table. TextF3 is a field in the Level2 table. Level2 is a child of Level1.

Level1 contains three rows. In the first two rows the VList value is “No”. In the third row the value is “Yes”.

What I am noticing is that, since the IF condition is false in the first two rows, the merge fields in the true condition stay in the document. I need to find a way to get rid of the merge fields after each row in Level1 is processed. This is necessary because we are intercepting the merge field processing (by implementing IMergeFieldCallback) so that we manipulate the value before storing it in the document. We use the MoveToMergeField method to replace the merge field with the manipulated value. I put a break point in our code and the code only gets executed for the third row. Because the merge fields are not being removed by the time we get to the third row there are three MERGEFIELDs in the document with a field of TextF3, so when I call the MoveToMergeField(“TextF3”), it goes to the wrong one (it goes to the first row when I want the value to be placed in the third row). I need to find a way to get rid of the MERGEFIELD when the IF condition is false.

How can I have the Mail Merge process eliminate the IF condition after every row.

I do have the CleanupOptions set to remove Contained Fields (.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveContainingFields;) but that does not seem to take care of the problem.

Hi,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  1. Your input Word document you're getting this problem with.
  2. The Aspose.Words generated output document which shows the undesired behavior.
  3. Your expected Word document. You can use Microsoft Word to create this document.
  4. Please create a standalone/runnable simple console application that helps me reproduce your problem on my end and attach it here for testing.

As soon as you get these pieces of information ready for me, I'll start investigation into your issue and provide you more information.

Best regards,

As requested I have attached a sample application that reproduces my issue.

Notice that if the first row of the region the False condition is executed which makes the MERGEFIELD for the "true" condition remain in the document. So when the second record is processed and the MoveToMergeField method is executed (line 51 of MailMergeDocument class), it moves to the MERGEFIELD in the first row.

I have included an actual results document (ActualOutput.docx) and an expected results document (ExpectedOutput.docx).

A second question, how do I get rid of the IF condition out of the output document. As you can see I am including the MailMergeCleanupOptions.RemoveContainedFields but that does not seem to eliminate it.

Hi,


Thanks for the additional information. Please remove the following line of code from the constructor of the “MailMergeDocument” class and paste it in inside “MailMergeDocument.Merge” method just before the “MailMerge.Execute” and “MailMerge.ExecuteWithRegions” method calls:

Builder.Document.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveUnusedRegions

| MailMergeCleanupOptions.RemoveUnusedFields
| MailMergeCleanupOptions.RemoveContainingFields;


I hope, this helps.

Best regards,

Thanks for the quick feedback. That solved the problem based on the example I sent you but now I have an example that is a bit more complicated. It is the same problem but now I have 3 levels instead of just 2.

When I run this attached code I get an Object Reference Exception when the MailMergeCleanupOptions.RemoveContainingFields is included in the Cleanup Options (you will have to add it to the line 63 of MailMergeDocument because I removed it to get past the exception).

If I remove the MailMergeCleanupOptions.RemoveContainingFields then it does not get the exception but the output is not what I would expect.

It looks like that Aspose Mail Merge processes all child level rows first and then goes back and processes all grandchild level rows. But, in this case it causes multiple grandchild TableStart and TableEnd merge fields, which I think it what is causing it not to cleanup the grandchild rows for each child row, and thereby putting the MERGEFIELD value in the wrong row.

Here is another example. I am not sure if it is the same problem as the previous example of a different problem. I took out the MailMergeCleanupOptions.RemoveContainingFields so you can see what is happening. The merge process is putting the fields in wrong row, even with the RemoveContainingFields option there it looks like it is putting the fields in the wrong row.

Thanks for your help.

Hi,


Thanks for the additional information. We are working over your query and will get back to you soon.

Best regards,

Hi,


Thanks for being patient. The exception “Object reference not set to an instance of an object error” is not reproducible when using the latest version of Aspose.Words 13.12.0. However, in your case, when a nested mail merge region is placed inside an IF field, for some reasons, Aspose.Words is not generating the expected output. I have logged this issue in our bug tracking system. The ID of your issue is WORDSNET-9610. Your request has been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,

I know it is often difficult to provide time frames but we have several clients putting a lot of pressure on us to get this fixed. Do you have any time frame in mind of when this will be fixed?

Hi,


Thanks for your inquiry. Unfortunately, this issue is not resolved yet. This issue is pending for analysis and is in the queue. The ETA is not available. Once the analysis of this issue is completed, we will then be able to share the ETA. We apologize for your inconvenience.

Best regards,

Hi Bob,


Thanks for your email and letting us know of your concerns.

Your issue currently has a normal priority and is unfortunately not resolved yet. Our development team is currently doing analysis of this issue but I am afraid, we can’t promise a delivery date for the fix yet. Although we try our best to deal with every customer request in a timely fashion, we unfortunately cannot guarantee a delivery date to every customer issue. Our developers work on issues on a first come, first served basis. We feel this is the fairest and most appropriate way to satisfy the needs of the majority of our customers.

That being said we fully understand that you are keen to get a fix to your particular issue; you can purchase Priority/Enterprise support to escalate your issue(s). Please visit the following link for more information:
http://www.aspose.com/corporate/services/support-options.aspx

Best regards,

Hi Bob,


Thanks for your email. Please use the “Priority Support” or “Enterprise Support” account of your company and post your query in Aspose.Priority.Support or Aspose.Enterprise.Support forum to be able to raise the priority of this issue. Note that the owner account of this thread “Jeeno” doesn’t have permissions to access the above mentioned forums. You can also contact our sales team in case you require any further help in this regard.

Best regards,

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


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

I can see how you have fixed the issue because you are now merging both sets of fields, but now there seems to be another issue. I have attached a sample application (IFRegionProblem.zip) that shows the issue. Seems like the Builder.InsertHtml method is no longer working as expected. The second line has the value showing correctly in the document but the first value is showing the html tags.

Edit: I did a bit more investigation and it looks like the only problem is that the InsertHtml is not working the first time. Every row except the first row has the Html rendered correctly.

Edit: On a side note...In my opinion you implemented the fix incorrectly. Instead of executing the merge fields in the false condition you should just remove them. I believe there will be times when exceptions will occur when attempting to process information in a condition that is not expected to occur. Usually, in any calculation, when a false condition occurs the process would be ignore. Maybe in the next version you can provide an option to allow us to just remove the fields.

Hi,


Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi,


Thanks for being patient. Please see the following changes in your code to fix this issue:

public void FieldMerging(FieldMergingArgs args)

{

ProcessItem(args.Field.GetFieldCode(), args.Field, args.FieldValue);

}

private void ProcessItem(string fieldCode, Field field, object fieldValue)

{

if (fieldCode.ToLowerInvariant().Contains("html"))

{

Node refNode = field.Remove();

Builder.MoveTo(refNode);

Builder.InsertHtml(fieldValue.ToString());

}

}


I hope, this helps.

Best regards,