Error while merging document having OR condition in If statement. "Index and length must refer to a location within the string. Parameter name: length"

Error while merging document having OR condition in If statement. "Index and length must refer to a location within the string. Parameter name: length"


my code is as given below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Words;
using System.Data;
using Aspose.Words.Reporting;

namespace AsposeConsoleApp
{
class Program
{
static void Main(string[] args)
{
License lic = new License();
lic.SetLicense(@“C:\Users\jm39360\Downloads\Aspose.Total.lic”);
Document doc = new Document(@“C:\Users\jm39360\Desktop\Approach2Template.dotx”);
DataTable data = GetmergeData();
doc.MailMerge.CleanupOptions = doc.MailMerge.CleanupOptions & ~MailMergeCleanupOptions.RemoveUnusedFields | ~MailMergeCleanupOptions.RemoveUnusedRegions
| ~MailMergeCleanupOptions.RemoveEmptyParagraphs | ~MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.ExecuteWithRegions(data);
doc.Save(“out.docx”);
}

private static DataTable GetmergeData()
{
DataTable table = new DataTable(“Customer”);
table.Columns.Add(“Currency”, typeof(string));
DataRow row = table.NewRow();
row[“Currency”] = “GBP”;
table.Rows.Add(row);
return table;
}


}
}

have valid Aspose.total license.

Red lined code gives error.

my template has simple text as given below:

{ MERGEFIELD TableStart:Customer}
{if { = OR({ COMPARE { MERGEFIELD Currencey } = “GBP”),{ COMPARE { MERGEFIELD Currencey } = “HKD”)} = 1 “test”}

{ MERGEFIELD TableEnd:Customer }




Hi Jatin,


Thanks for your inquiry.

I have tested the scenario and have not found the shared issue while using latest version of Aspose.Words for .NET. I have attached the input and output document with this post for your kind reference.

In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.2.0) from here and let us know how it goes on your side. If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.