Problems with 'if' statement

attached is my test code.

in the template is a table with a conditional if statement.
I cannot get the if statement to print any text.
please help.fields.zip (5.4 MB)

@conniem,

The problem occurs because the field codes (press ALT+F9 to toggle field codes in MS Word) in your final “result.docx” are not well formed. For example, it contains an IF field like this:

{ IFN="Y"asdlka;sjdf }

To fix this issue, you should write the field code like this"

{ IF "N"="Y" "asdlka;sjdf" }

fields.zip (12.1 KB)
I have fixed that.
however, on the print out, (result.docx), only one word of ‘d.narr’ is showing, even through the text (see dvalues.txt) has multiple words.

@conniem,

Unfortunately, your question is not clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your usecase and problem. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly. Thanks for your cooperation.

given the template, the data, and the code already provided, the ‘if’ condition is only printing out one word, even though multiple words are sent to builder.write in the following section of code:

  static void FillMergeFields(Document aDoc, string dataList, string fieldList)
  {
     string[] rowData = dataList.Split((char)24);
     string[] fList = fieldList.Split((char)24);
     string rfield;

     DocumentBuilder Builder = new DocumentBuilder(aDoc);
     if (rowData.Length >= fList.Length)
     {
        for (int i = 0; i < fList.Length; i++)
        {
           rfield = fList[i];
           if (Builder.MoveToMergeField(rfield))
           {
              if (rowData[i].Trim() != "")
                 Builder.Write(rowData[i].Trim());
              else
                 Builder.Write(" ");
           }
        }               
     }            
  }

@conniem,

Please also provide an updated standalone Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size. Thanks for your cooperation.

I’m not using java. I’m using c#

fields.zip (16.8 KB)
just in case the source that I already sent was misplaced

@conniem,

Please use the mail merge with regions feature of Aspose.Words. I have modified your template document (input.zip (6.4 KB)) and please build logic on the following code to get the desired output.

Document doc = new Document("E:\\Temp\\fields (2)\\input.dot");

string s = "E:\\temp\\fields (2)";
string fieldList = File.ReadAllText(s + "\\dfields.txt");
string dataList = File.ReadAllText(s + "\\dvalues.txt");

string[] rowData = dataList.Split(new char[] { ControlChar.LineFeedChar });
string[] aline = fieldList.Split((char)24);

DataTable dt = new DataTable("DT");
for (int i = 0; i < aline.Length; i++)
{
    string colName = aline[i];
    dt.Columns.Add(colName);
}

for (int i = 0; i < rowData.Length; i++)
{
    string[] values = rowData[i].Split((char)24);
    if (values.Length == 5)
    {
        DataRow row = dt.NewRow();
        for (int x = 0; x < 5; x++)
        {
            row[x] = values[x].Trim();
        }
        dt.Rows.Add(row);
    }
}

doc.MailMerge.ExecuteWithRegions(dt);

doc.Save("E:\\Temp\\fields (2)\\19.4.docx");

I am prevented from using mail merge with regions, because we have hundreds of users who in turn, have hundreds of templates, and none of those templates have regions. I am constrained to making this work without changing the templates.

Is there a reason the builder cannot output multiple words in the if condition?
How can I make that work?

is there a method to insert a region in a data row of a table, using code? so that region starts before the first merge field in the row, and ends after the last merge field in the row?

@conniem,

We suggest you please use the mail merge with regions feature of Aspose.Words as it provides more flexibility e.g. you can implement IFieldMergingCallback interface if you want to do some custom operations during mail merge. Please also refer to the following article:
Inserting Merge Field into a Document using DOM

Since, again, it’s not really feasible for me to manually update the hundreds of templates from client sites one at a time, how can I insert regions into the existing tables in the existing templates in the proper places?
Otherwise, how can I make builder work for this instance, where it will not display multiple words in an if condition?

@conniem,

Please see these input/output Word documents (Docs.zip (11.9 KB)) and try running the following code:

Document doc = new Document("E:\\fields (2)\\test.dot");
DocumentBuilder builder = new DocumentBuilder(doc);

ArrayList list = new ArrayList();
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldMergeField)
    {
        FieldMergeField mf = (FieldMergeField)field;
        list.Add(mf);
    }
}

FieldMergeField mfFirst = (FieldMergeField)list[0];
FieldMergeField mfLast = (FieldMergeField)list[list.Count - 1];

builder.MoveTo(mfFirst.Start);
FieldMergeField startRegion = (FieldMergeField)builder.InsertField(FieldType.FieldMergeField, false);
startRegion.FieldName = "TableStart:DT";
startRegion.Update();

builder.MoveTo(mfLast.End.GetAncestor(NodeType.Paragraph));
FieldMergeField endRegion = (FieldMergeField)builder.InsertField(FieldType.FieldMergeField, false);
endRegion.FieldName = "TableEnd:DT";
endRegion.Update();

doc.Save("E:\\fields (2)\\19.4.docx");

Hope, this helps.

Thank you. between this and the previous answer on how to fill in the data, the problem is fixed.

I appreciate your help

@conniem,

It is great that you were able to resolve this issue on your end. Please let us know any time you have any further queries.

fields.zip (17.3 KB)

attached is the latest interation
using result.docx, if you toggle the second merge field in the first column of the table, (the one with the ‘if’ statement in it), you will see that you can toggle the field codes there (but can’t in the first field), and that the ‘unused’ field is still there
is there a way to make that not happen, because later, in my real code, when I get the field list for the document, that field is showing up (d.ltext) in the document field list.

@conniem,

Please check the following article:
How to Remove Unmerged Fields, Empty Paragraphs and Unmerged Regions

I have also toggled the field codes in result.docx and they look like below:

Please also provide a screenshot highlighting the problematic areas (along with problem description) here for any further testing. Thanks for your cooperation.